summaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/features/nativefieldmatchfeature.cpp
blob: 499fa2bdc00a413f9805168f0f4fe61824e1ff14 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "nativefieldmatchfeature.h"
#include "valuefeature.h"
#include "utils.h"
#include <vespa/searchlib/fef/fieldinfo.h>
#include <vespa/searchlib/fef/indexproperties.h>
#include <vespa/searchlib/fef/itablemanager.h>
#include <vespa/searchlib/fef/properties.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <vespa/vespalib/util/stash.h>

using namespace search::fef;

namespace search::features {

const uint32_t NativeFieldMatchParam::NOT_DEF_FIELD_LENGTH(std::numeric_limits<uint32_t>::max());

NativeFieldMatchExecutorSharedState::NativeFieldMatchExecutorSharedState(const IQueryEnvironment& env,
                                                                         const NativeFieldMatchParams& params)
    : fef::Anything(),
      _params(params),
      _query_terms(),
      _divisor(0)
{
    QueryTermHelper queryTerms(env);
    for (const QueryTerm & qtTmp : queryTerms.terms()) {
        if (qtTmp.termData()->getWeight().percent() != 0) // only consider query terms with contribution
        {
            MyQueryTerm qt(qtTmp);
            using FRA = search::fef::ITermFieldRangeAdapter;
            uint32_t totalFieldWeight = 0;
            for (FRA iter(*qt.termData()); iter.valid(); iter.next()) {
                const ITermFieldData& tfd = iter.get();
                uint32_t fieldId = tfd.getFieldId();
                if (_params.considerField(fieldId)) { // only consider fields with contribution
                    totalFieldWeight += _params.vector[fieldId].fieldWeight;
                    qt.handles().emplace_back(tfd.getHandle(), &tfd);
                }
            }
            if (!qt.handles().empty()) {
                _query_terms.push_back(qt);
                _divisor += (qt.significance() * qt.termData()->getWeight().percent() * totalFieldWeight);
            }
        }
    }
}

NativeFieldMatchExecutorSharedState::~NativeFieldMatchExecutorSharedState() = default;

feature_t
NativeFieldMatchExecutor::calculateScore(const MyQueryTerm &qt, uint32_t docId)
{
    feature_t termScore = 0;
    for (size_t i = 0; i < qt.handles().size(); ++i) {
        TermFieldHandle tfh = qt.handles()[i].first;
        const TermFieldMatchData *tfmd = _md->resolveTermField(tfh);
        const NativeFieldMatchParam & param = _params.vector[tfmd->getFieldId()];
        if (tfmd->getDocId() == docId) { // do we have a hit
            FieldPositionsIterator pos = tfmd->getIterator();
            if (pos.valid()) {
                uint32_t fieldLength = getFieldLength(param, pos.getFieldLength());
                termScore +=
                    ((getFirstOccBoost(param, pos.getPosition(), fieldLength) * param.firstOccImportance) +
                     (getNumOccBoost(param, pos.size(), fieldLength) * (1 - param.firstOccImportance))) *
                    param.fieldWeight / param.maxTableSum;
            }
        }
    }
    termScore *= (qt.significance() * qt.termData()->getWeight().percent());
    return termScore;
}

NativeFieldMatchExecutor::NativeFieldMatchExecutor(const NativeFieldMatchExecutorSharedState& shared_state)
    : FeatureExecutor(),
      _params(shared_state.get_params()),
      _queryTerms(shared_state.get_query_terms()),
      _divisor(shared_state.get_divisor()),
      _md(nullptr)
{
    for (const auto& qt : _queryTerms) {
        for (const auto& handle : qt.handles()) {
            // Record that we need normal term field match data
            (void) handle.second->getHandle(MatchDataDetails::Normal);
        }
    }
}

void
NativeFieldMatchExecutor::execute(uint32_t docId)
{
    feature_t score = 0;
    for (size_t i = 0; i < _queryTerms.size(); ++i) {
        score += calculateScore(_queryTerms[i], docId);
    }
    if (_divisor > 0) {
        score /= _divisor;
    }
    outputs().set_number(0, score);
}

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

NativeFieldMatchBlueprint::NativeFieldMatchBlueprint() :
    Blueprint("nativeFieldMatch"),
    _params(),
    _defaultFirstOcc("expdecay(8000,12.50)"),
    _defaultNumOcc("loggrowth(1500,4000,19)"),
    _shared_state_key()
{
}

NativeFieldMatchBlueprint::~NativeFieldMatchBlueprint() = default;

void
NativeFieldMatchBlueprint::visitDumpFeatures(const IIndexEnvironment & env,
                                             IDumpFeatureVisitor & visitor) const
{
    (void) env;
    visitor.visitDumpFeature(getBaseName());
}

Blueprint::UP
NativeFieldMatchBlueprint::createInstance() const
{
    return std::make_unique<NativeFieldMatchBlueprint>();
}

bool
NativeFieldMatchBlueprint::setup(const IIndexEnvironment & env,
                                 const ParameterList & params)
{
    vespalib::asciistream shared_state_key_builder;
    _params.resize(env.getNumFields());
    FieldWrapper fields(env, params, FieldType::INDEX);
    vespalib::string defaultFirstOccImportance = env.getProperties().lookup(getBaseName(), "firstOccurrenceImportance").get("0.5");
    shared_state_key_builder << "fef.nativeFieldMatch[";
    bool first_field = true;
    for (uint32_t i = 0; i < fields.getNumFields(); ++i) {
        const FieldInfo * info = fields.getField(i);
        uint32_t fieldId = info->id();
        NativeFieldMatchParam & param = _params.vector[fieldId];
        param.field = true;
        if ((param.firstOccTable =
             util::lookupTable(env, getBaseName(), "firstOccurrenceTable", info->name(), _defaultFirstOcc)) == NULL)
        {
            return false;
        }
        if ((param.numOccTable =
             util::lookupTable(env, getBaseName(), "occurrenceCountTable", info->name(), _defaultNumOcc)) == NULL)
        {
            return false;
        }
        param.fieldWeight = indexproperties::FieldWeight::lookup(env.getProperties(), info->name());
        if (param.fieldWeight == 0 ||
            info->isFilter())
        {
            param.field = false;
        }
        Property afl = env.getProperties().lookup(getBaseName(), "averageFieldLength", info->name());
        if (afl.found()) {
            param.averageFieldLength = util::strToNum<uint32_t>(afl.get());
        }

        param.firstOccImportance = util::strToNum<feature_t>
            (env.getProperties().lookup(getBaseName(), "firstOccurrenceImportance", info->name()).
             get(defaultFirstOccImportance));

        if (NativeRankBlueprint::useTableNormalization(env)) {
            const Table * fo = param.firstOccTable;
            const Table * no = param.numOccTable;
            if (fo != NULL && no != NULL) {
                double value = (fo->max() * param.firstOccImportance) +
                    (no->max() * (1 - param.firstOccImportance));
                _params.setMaxTableSums(fieldId, value);
            }
        }
        if (param.field) {
            env.hintFieldAccess(fieldId);
            if (first_field) {
                first_field = false;
            } else {
                shared_state_key_builder << ",";
            }
            shared_state_key_builder << info->name();
        }
    }
    shared_state_key_builder << "]";
    _shared_state_key = shared_state_key_builder.str();
    _params.minFieldLength = util::strToNum<uint32_t>(env.getProperties().lookup
                                                      (getBaseName(), "minFieldLength").get("6"));

    describeOutput("score", "The native field match score");
    return true;
}

FeatureExecutor &
NativeFieldMatchBlueprint::createExecutor(const IQueryEnvironment &env, vespalib::Stash &stash) const
{
    auto *shared_state = dynamic_cast<const NativeFieldMatchExecutorSharedState *>(env.getObjectStore().get(_shared_state_key));
    if (shared_state == nullptr) {
        shared_state = &stash.create<NativeFieldMatchExecutorSharedState>(env, _params);
    }
    if (shared_state->empty()) {
        return stash.create<SingleZeroValueExecutor>();
    } else {
        return stash.create<NativeFieldMatchExecutor>(*shared_state);
    }
}

void
NativeFieldMatchBlueprint::prepareSharedState(const IQueryEnvironment &queryEnv, IObjectStore &objectStore) const {
    QueryTermHelper::lookupAndStoreQueryTerms(queryEnv, objectStore);
    if (objectStore.get(_shared_state_key) == nullptr) {
        objectStore.add(_shared_state_key, std::make_unique<NativeFieldMatchExecutorSharedState>(queryEnv, _params));
    }
}

}