aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/features/native_dot_product_feature.cpp
blob: 9472814f58190a4ea2f2b755df7f2b55bc445efe (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "native_dot_product_feature.h"
#include "utils.h"
#include <vespa/vespalib/util/stash.h>

using namespace search::fef;

namespace search::features {

NativeDotProductExecutor::NativeDotProductExecutor(const search::fef::IQueryEnvironment &env)
    : FeatureExecutor(),
      _pairs(),
      _md(nullptr)
{
    for (uint32_t i = 0; i < env.getNumTerms(); ++i) {
        const search::fef::ITermData *td = env.getTerm(i);
        auto weight = td->getWeight();
        for (size_t f = 0; f < td->numFields(); ++f) {
            auto handle = td->field(f).getHandle();
            if (handle != search::fef::IllegalHandle) {
                _pairs.emplace_back(handle, weight);
            }
        }
    }
}

NativeDotProductExecutor::NativeDotProductExecutor(const search::fef::IQueryEnvironment &env, uint32_t fieldId)
    : FeatureExecutor(),
      _pairs(),
      _md(nullptr)
{
    for (uint32_t i = 0; i < env.getNumTerms(); ++i) {
        search::fef::TermFieldHandle handle = util::getTermFieldHandle(env, i, fieldId);
        if (handle != search::fef::IllegalHandle) {
            _pairs.push_back(std::make_pair(handle, env.getTerm(i)->getWeight()));
        }
    }
}

void
NativeDotProductExecutor::execute(uint32_t docId)
{
    feature_t output = 0.0;
    for (uint32_t i = 0; i < _pairs.size(); ++i) {
        const TermFieldMatchData *tfmd = _md->resolveTermField(_pairs[i].first);
        if (tfmd->getDocId() == docId) {
            output += (tfmd->getWeight() * (int32_t)_pairs[i].second.percent());
        }
    }
    outputs().set_number(0, output);
}

void
NativeDotProductExecutor::handle_bind_match_data(const fef::MatchData &md)
{
    _md = &md;
}

//-----------------------------------------------------------------------------

NativeDotProductBlueprint::~NativeDotProductBlueprint() = default;

bool
NativeDotProductBlueprint::setup(const IIndexEnvironment &,
                                 const ParameterList &params)
{
    if (params.size() > 0) {
        _field = params[0].asField();
    }
    describeOutput("out", "dot product between query term weights and match weights");
    return true;
}

FeatureExecutor &
NativeDotProductBlueprint::createExecutor(const IQueryEnvironment &queryEnv, vespalib::Stash &stash) const
{
    if (_field) {
        return stash.create<NativeDotProductExecutor>(queryEnv, _field->id());
    } else {
        return stash.create<NativeDotProductExecutor>(queryEnv);
    }
}

}