aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/features/flow_completeness_feature.cpp
blob: 166280b289d9110633c8ba1b8ba0007ee528c470 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "flow_completeness_feature.h"
#include <vespa/searchlib/fef/itermdata.h>
#include <vespa/searchlib/fef/featurenamebuilder.h>
#include <vespa/searchlib/fef/properties.h>
#include <vespa/searchlib/fef/indexproperties.h>
#include <vespa/vespalib/stllike/hash_map.h>
#include <vespa/vespalib/locale/c.h>
#include <vespa/vespalib/util/stash.h>

#include <cassert>

#include <vespa/log/log.h>
LOG_SETUP(".features.flowcompleteness");

namespace search::features {

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

FlowCompletenessExecutor::FlowCompletenessExecutor(const fef::IQueryEnvironment &env,
                                                   const FlowCompletenessParams &params)
    : _params(params),
      _terms(),
      _queue(),
      _sumTermWeight(0)
{
    for (uint32_t i = 0; i < env.getNumTerms(); ++i) {
        LOG(spam, "consider term %u", i);
        const fef::ITermData *termData = env.getTerm(i);
        LOG(spam, "term %u weight %u", i, termData->getWeight().percent());
        if (termData->getWeight().percent() != 0) { // only consider query terms with contribution
            using FRA = fef::ITermFieldRangeAdapter;
            uint32_t j = 0;
            for (FRA iter(*termData); iter.valid(); iter.next()) {
                const fef::ITermFieldData &tfd = iter.get();
                LOG(spam, "term %u field data %u for field id %u (my field id %u)",
                    i, j++, tfd.getFieldId(), _params.fieldId);
                if (tfd.getFieldId() == _params.fieldId) {
                    int termWeight = termData->getWeight().percent();
                    _sumTermWeight += termWeight;
                    _terms.push_back(Term(tfd.getHandle(), termWeight));
                }
            }
        }
    }
    LOG(spam, "added %zu terms", _terms.size());
}

namespace {
using TermIdxList = std::vector<uint32_t>;
using PosList = std::vector<uint32_t>;

using TermIdxMap = vespalib::hash_map<uint32_t, uint32_t>;

struct State {
    int       elementWeight;
    uint32_t  elementLength;
    uint32_t  matchedTerms;
    int       sumTermWeight;

    std::vector<PosList> positionsForTerm;
    uint32_t             posLimit;
    PosList              matchedPosForTerm;
    TermIdxMap           matchedTermForPos; // maps pos -> term

    double    score;
    double    flow;
    feature_t completeness;
    feature_t fieldCompleteness;
    feature_t queryCompleteness;

    State(int weight, uint32_t length)
        : elementWeight(weight), elementLength(length),
          matchedTerms(0), sumTermWeight(0),
          posLimit(0),
          score(0.0), flow(0.0),
          completeness(0.0), fieldCompleteness(0.0), queryCompleteness(0.0) {}
    ~State() { }

    void addMatch(int termWeight) {
        ++matchedTerms;
        sumTermWeight += termWeight;
    }

    struct Path {
        std::vector<uint32_t> path;
        bool operator< (const Path& other) const {
            return path.size() < other.path.size();
        }
    };

    Path bfs(vespalib::PriorityQueue<Path> &queue)
    {
        TermIdxList seen(matchedTerms, 0);
        while (!queue.empty()) {
            Path firstP = queue.front();
            queue.pop_front();
            uint32_t startTerm = firstP.path.back();
            seen[startTerm] = 1;
            PosList &edges = positionsForTerm[startTerm];
            for (size_t j = 0; j < edges.size(); ++j) {
                Path nextP = firstP;
                uint32_t pos = edges[j];
                nextP.path.push_back(pos);
                TermIdxMap::const_iterator it = matchedTermForPos.find(pos);
                if (it == matchedTermForPos.end()) {
                    return nextP;
                } else {
                    uint32_t nextTerm = it->second;
                    if (seen[nextTerm] == 0) {
                        seen[nextTerm] = 1;
                        nextP.path.push_back(nextTerm);
                        queue.push(nextP);
                    }
                }
            }
        }
        return Path();
    }

    int findMatches() {
        vespalib::PriorityQueue<Path> q;

        for (size_t i = 0; i < matchedTerms; ++i) {
            if (matchedPosForTerm[i] == IllegalPosId) {
                Path p;
                p.path.push_back(i);
                q.push(p);
            }
        }
        if (q.empty()) {
            return 0;
        }
        Path p = bfs(q);
        if (p.path.size() == 0) {
            return 0;
        }
        while (p.path.size() > 1) {
            uint32_t pos = p.path.back();
            assert(pos < posLimit);
            p.path.pop_back();
            uint32_t tix = p.path.back();
            assert(tix < matchedTerms);
            p.path.pop_back();
            matchedTermForPos[pos] = tix;
            matchedPosForTerm[tix] = pos;
        }
        assert(p.path.size() == 0);
        return 1;
    }

    int findSimpleMatches() {
        int found = 0;
        for (size_t tix = 0; tix < matchedTerms; ++tix) {
            assert(matchedPosForTerm[tix] == IllegalPosId);
            assert(positionsForTerm[tix].size() > 0);
            uint32_t pos = positionsForTerm[tix][0];
            assert(pos < posLimit);

            TermIdxMap::const_iterator it = matchedTermForPos.find(pos);
            if (it == matchedTermForPos.end()) {
                ++found;
                matchedTermForPos[pos] = tix;
                matchedPosForTerm[tix] = pos;
            }
        }
        return found;
    }

    void calculateScore(uint32_t queryTerms, double factor) {
        matchedPosForTerm.resize(matchedTerms, IllegalPosId);
        int more = findSimpleMatches();
        flow += more;
        while ((more = findMatches()) > 0) {
            flow += more;
        }
        queryCompleteness = (flow / (double)queryTerms);
        fieldCompleteness = (flow / (double)elementLength);
        completeness = (fieldCompleteness * factor) +
                       (queryCompleteness * (1 - factor));
        score = completeness * (double)sumTermWeight;
    }
};

}


void
FlowCompletenessExecutor::execute(uint32_t)
{
    assert(_queue.empty());
    for (size_t i = 0; i < _terms.size(); ++i) {
        const fef::TermFieldMatchData *tfmd = _md->resolveTermField(_terms[i].termHandle);
        Item item(i, tfmd->begin(), tfmd->end());
        LOG(spam, "found tfmd item with %zu positions", (item.end - item.pos));
        if (item.pos != item.end) {
            _queue.push(item);
        }
    }
    State best(0, 0);
    while (!_queue.empty()) {
        Item &start = _queue.front();
        uint32_t elementId = start.elemId;
        LOG_ASSERT(start.pos != start.end);
        State state(start.pos->getElementWeight(), start.pos->getElementLen());

        while (!_queue.empty() && _queue.front().elemId == elementId) {
            Item &item = _queue.front();

            // update state
            state.positionsForTerm.push_back(PosList());
            while (item.pos != item.end && item.pos->getElementId() == elementId) {
                uint32_t pos = item.pos->getPosition();
                state.positionsForTerm.back().push_back(pos);
                state.posLimit = std::max(state.posLimit, pos + 1);
                ++item.pos;
            }
            state.addMatch(_terms[item.termIdx].termWeight);

            // adjust item and its place in queue
            if (item.pos == item.end) {
                _queue.pop_front();
            } else {
                item.elemId = item.pos->getElementId();
                _queue.adjust();
            }
        }
        state.calculateScore(_terms.size(), _params.fieldCompletenessImportance);
        if (state.score > best.score) {
            best = state;
        }
    }
    outputs().set_number(0, best.completeness);
    outputs().set_number(1, best.fieldCompleteness);
    outputs().set_number(2, best.queryCompleteness);
    outputs().set_number(3, best.elementWeight);
    outputs().set_number(4, _params.fieldWeight);
    outputs().set_number(5, best.flow);

}

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

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

FlowCompletenessBlueprint::FlowCompletenessBlueprint()
    : Blueprint("flowCompleteness"),
      _output(),
      _params()
{
    _output.push_back("completeness");
    _output.push_back("fieldCompleteness");
    _output.push_back("queryCompleteness");
    _output.push_back("elementWeight");
    _output.push_back("weight");
    _output.push_back("flow");
}

void
FlowCompletenessBlueprint::visitDumpFeatures(const fef::IIndexEnvironment &env,
                                             fef::IDumpFeatureVisitor &visitor) const
{
#ifdef notyet
    for (uint32_t i = 0; i < env.getNumFields(); ++i) {
        const fef::FieldInfo &field = *env.getField(i);
        if (field.type() == fef::FieldType::INDEX) {
            if (!field.isFilter()) {
                fef::FeatureNameBuilder fnb;
                fnb.baseName(getBaseName()).parameter(field.name());
                for (size_t out = 0; out < _output.size(); ++out) {
                    visitor.visitDumpFeature(fnb.output(_output[out]).buildName());
                }
            }
        }
    }
#else
    (void)env;
    (void)visitor;
#endif
}

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

bool
FlowCompletenessBlueprint::setup(const fef::IIndexEnvironment &env,
                                 const fef::ParameterList &params)
{
    const fef::FieldInfo *field = params[0].asField();

    _params.fieldId = field->id();
    const fef::Properties &lst = env.getProperties();
    fef::Property obj = lst.lookup(getName(), "fieldCompletenessImportance");
    if (obj.found()) {
        _params.fieldCompletenessImportance = vespalib::locale::c::atof(obj.get().c_str());
    }
    _params.fieldWeight = fef::indexproperties::FieldWeight::lookup(lst, field->name());

    describeOutput(_output[0], "combined completeness for best scored element");
    describeOutput(_output[1], "best scored element completeness");
    describeOutput(_output[2], "query completeness for best scored element");
    describeOutput(_output[3], "element weight of best scored element");
    describeOutput(_output[4], "field weight");
    describeOutput(_output[5], "query terms matching in best element (measured by flow)");
    return true;
}

fef::FeatureExecutor &
FlowCompletenessBlueprint::createExecutor(const fef::IQueryEnvironment &env, vespalib::Stash &stash) const
{
    return stash.create<FlowCompletenessExecutor>(env, _params);
}

}