aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/query/streaming/dot_product_term.cpp
blob: bfc66d14e34ed22fb1b1c3c4e2e96c7236171304 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "dot_product_term.h"
#include <vespa/searchlib/fef/itermdata.h>
#include <vespa/searchlib/fef/matchdata.h>
#include <vespa/vespalib/stllike/hash_map.h>

using search::fef::ITermData;
using search::fef::MatchData;

namespace search::streaming {

DotProductTerm::DotProductTerm(std::unique_ptr<QueryNodeResultBase> result_base, const string & index, uint32_t num_terms)
    : MultiTerm(std::move(result_base), index, num_terms)
{
}

DotProductTerm::~DotProductTerm() = default;

void
DotProductTerm::build_scores(Scores& scores) const
{
    HitList hl_store;
    for (const auto& term : _terms) {
        auto& hl = term->evaluateHits(hl_store);
        for (auto& hit : hl) {
            scores[hit.field_id()] += ((int64_t)term->weight().percent()) * hit.element_weight();
        }
    }
}

void
DotProductTerm::unpack_scores(Scores& scores, std::optional<double> score_threshold, uint32_t docid, const fef::ITermData& td, fef::MatchData& match_data)
{
    auto num_fields = td.numFields();
    for (uint32_t field_idx = 0; field_idx < num_fields; ++field_idx) {
        auto& tfd = td.field(field_idx);
        auto field_id = tfd.getFieldId();
        if (scores.contains(field_id)) {
            auto score = scores[field_id];
            if (!score_threshold.has_value() || score_threshold.value() < score) {
                auto handle = tfd.getHandle();
                if (handle != fef::IllegalHandle) {
                    auto tmd = match_data.resolveTermField(tfd.getHandle());
                    tmd->setFieldId(field_id);
                    tmd->setRawScore(docid, score);
                }
            }
        }
    }
}

void
DotProductTerm::unpack_match_data(uint32_t docid, const ITermData& td, MatchData& match_data, const fef::IIndexEnvironment&)
{
    Scores scores;
    build_scores(scores);
    unpack_scores(scores, std::nullopt, docid, td, match_data);
}

}