aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/features/fieldmatch/computer.cpp
blob: 7f97e3a4ca26a5a61344e7d720e481bde85c94d3 (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "computer.h"
#include "computer_shared_state.h"
#include <vespa/searchlib/features/utils.h>
#include <vespa/searchlib/fef/phrase_splitter_query_env.h>
#include <vespa/searchlib/fef/phrasesplitter.h>
#include <vespa/searchlib/fef/properties.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/locale/c.h>
#include <set>

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

using namespace search::fef;

namespace search::features::fieldmatch {

Computer::Computer(const ComputerSharedState& shared_state, const PhraseSplitter& splitter)
    : _shared_state(shared_state),
      _splitter(splitter),
      _fieldId(_shared_state.get_field_id()),
      _params(_shared_state.get_params()),
      _useCachedHits(_shared_state.get_use_cached_hits()),
      _queryTerms(_shared_state.get_query_terms()),
      _queryTermFieldMatch(_queryTerms.size()),
      _totalTermWeight(_shared_state.get_total_term_weight()),
      _totalTermSignificance(_shared_state.get_total_term_significance()),
      _fieldLength(FieldPositionsIterator::UNKNOWN_LENGTH),
      _currentMetrics(this),
      _finalMetrics(this),
      _simpleMetrics(_shared_state.get_simple_metrics()),
      _segments(),
      _alternativeSegmentationsTried(0),
      _cachedHits(_queryTerms.size())
{
    for (const auto &qt : _queryTerms) {
        // Record that we need normal term field match data
        (void) qt.termData()->lookupField(_fieldId)->getHandle(MatchDataDetails::Normal);
    }
    // num query terms searching in this field + 1
    _segments.reserve(getNumQueryTerms() + 1);
    for (uint32_t i = 0; i < (getNumQueryTerms() + 1); ++i) {
        _segments.emplace_back(std::make_shared<SegmentStart>(this, _currentMetrics));
    }
}

Computer::~Computer() = default;

void
Computer::reset(uint32_t docId)
{
    _currentMetrics.reset();
    _finalMetrics.reset();
    _simpleMetrics.resetMatchData();
    for (uint32_t i = 0; i < _segments.size(); ++i) {
        if (_segments[i].valid) {
            _segments[i].valid = false;
        }
    }
    _alternativeSegmentationsTried = 0;
    for (uint32_t i = 0; i < _cachedHits.size(); ++i) {
        if (_cachedHits[i].valid) {
            _cachedHits[i].valid = false;
        }
    }

    _fieldLength = FieldPositionsIterator::UNKNOWN_LENGTH;

    for (uint32_t i = 0; i < _queryTerms.size(); ++i) {
        const ITermData *td = _queryTerms[i].termData();
        const TermFieldMatchData *tfmd = _splitter.resolveTermField(_queryTerms[i].fieldHandle());
        if (tfmd->getDocId() != docId) { // only term match data if we have a hit
            tfmd = nullptr;
        } else {
            FieldPositionsIterator it = tfmd->getIterator();
            uint32_t fieldLength = it.getFieldLength();
            if (it.valid()) {
                _simpleMetrics.addMatchWithPosOcc(td->getWeight().percent());
                if (fieldLength == 0 || fieldLength == FieldPositionsIterator::UNKNOWN_LENGTH) {
                    _simpleMetrics.hasMatchWithInvalidFieldLength();
                }
            } else {
                _simpleMetrics.addMatch(td->getWeight().percent());
            }
            if (_fieldLength == FieldPositionsIterator::UNKNOWN_LENGTH) {
                _fieldLength = fieldLength; // save away the first valid field length
            }

            if (_useCachedHits && it.valid() && fieldLength != FieldPositionsIterator::UNKNOWN_LENGTH) {
                // cache the field position iterator in a bit vector for faster lookup in
                // findClosestInFieldBySemanticDistance()
                _cachedHits[i].bitvector.clear();
                _cachedHits[i].valid = true;
                if (_cachedHits[i].bitvector.size() < _fieldLength) {
                    _cachedHits[i].bitvector.resize(_fieldLength);
                }
                for (; it.valid(); it.next()) {
                    uint32_t fieldPos = it.getPosition();
                    if (__builtin_expect(fieldPos < _fieldLength, true))
                        _cachedHits[i].bitvector.setBit(fieldPos);
                    else {
                        handleError(fieldPos, docId);
                    }
                }
            }
        }
        _queryTermFieldMatch[i] = tfmd;
    }
}

void
Computer::handleError(uint32_t fieldPos, uint32_t docId) const
{
    static std::atomic<int> errcnt(0);
    if (errcnt < 1000) {
        errcnt++;
        const FieldInfo * finfo = _splitter.get_query_env().getIndexEnvironment().getField(getFieldId());
        LOG(debug, "Bad field position %u >= fieldLength %u for field '%s' document %u. "
                   "Document was probably refed during query (Ticket 7104969)",
                   fieldPos, _fieldLength,
                   finfo != nullptr ?  finfo->name().c_str() : "unknown field",
                   docId);
    }
}

const Metrics &
Computer::run()
{
    exploreSegments();
    return _finalMetrics;
}

int
Computer::findClosestInFieldBySemanticDistance(int i, int previousJ, uint32_t startSemanticDistance)
{
    if (_useCachedHits) {
        if (!_cachedHits[i].valid) {
            return -1; // not matched
        }

        const BitVector & hits = _cachedHits[i].bitvector;

        for (uint32_t distance = startSemanticDistance; distance < _fieldLength; distance++) {
            int j = semanticDistanceToFieldIndex(distance, previousJ);
            if (j < 0) {
                continue;
            }

            if (hits.testBit((uint32_t)j)) {
                return distance;
            }
        }
        return -1;
    }

    const TermFieldMatchData *termFieldMatch = _queryTermFieldMatch[i];
    if (termFieldMatch == nullptr) {
        return -1; // not matched
    }

    for (uint32_t distance = startSemanticDistance; distance < _fieldLength; distance++) {
        int j = semanticDistanceToFieldIndex(distance, previousJ);
        if (j < 0) {
            continue;
        }

        FieldPositionsIterator it = termFieldMatch->getIterator();
        while (it.valid() && it.getPosition() < (uint32_t)j) {
            it.next();
        }
        if (it.valid() && it.getPosition() == (uint32_t)j) {
            return distance;
        }
    }
    return -1;
}

int
Computer::semanticDistanceToFieldIndex(int semanticDistance, uint32_t zeroJ) const
{
    if (semanticDistance == -1) {
        return -1;
    }
    int firstSegmentLength = std::min(_params.getProximityLimit(), _fieldLength - zeroJ);
    int secondSegmentLength = std::min(_params.getProximityLimit(), zeroJ);
    if (semanticDistance < firstSegmentLength) {
        return zeroJ + semanticDistance;
    }
    else if (semanticDistance < firstSegmentLength + secondSegmentLength) {
        return zeroJ - semanticDistance - 1 + firstSegmentLength;
    }
    else if ((uint32_t)semanticDistance < _fieldLength - zeroJ + secondSegmentLength) {
        return zeroJ + semanticDistance - secondSegmentLength;
    }
    else {
        return _fieldLength - semanticDistance - 1;
    }
}

int
Computer::fieldIndexToSemanticDistance(int j, uint32_t zeroJ) const
{
    if (j == -1) {
        return -1;
    }
    uint32_t firstSegmentLength = std::min(_params.getProximityLimit(), _fieldLength - zeroJ);
    uint32_t secondSegmentLength = std::min(_params.getProximityLimit(), zeroJ);
    if ((uint32_t)j >= zeroJ) {
        if ((j - zeroJ) < firstSegmentLength) {
            return j - zeroJ; // 0..limit
        }
        else {
            return j - zeroJ + secondSegmentLength; // limit*2..field.length-zeroJ
        }
    }
    else {
        if ((zeroJ - j - 1) < secondSegmentLength) {
            return zeroJ - j + firstSegmentLength - 1; // limit..limit*2
        }
        else {
            return (zeroJ - j - 1) + _fieldLength - zeroJ; // field.length-zeroJ..
        }
    }
}

vespalib::string
Computer::toString() const
{
    return vespalib::make_string("Computer(%d query terms,%d field terms,%s)",
                                 getNumQueryTerms(), _fieldLength,
                                 _currentMetrics.toString().c_str());
}

void
Computer::exploreSegments()
{
    _segments[0].segment->reset(_currentMetrics);
    _segments[0].valid = true;
    SegmentStart *segment = _segments[0].segment.get();
    while (segment != nullptr) {
        _currentMetrics = segment->getMetrics(); // take a copy of the segment returned from the current segment.
        bool found = findAlternativeSegmentFrom(segment);
        if (!found) {
            segment->setOpen(false);
        }
        segment = findOpenSegment(segment->getI());
    }
    _finalMetrics = findLastStartPoint()->getMetrics();
    setOccurrenceCounts(_finalMetrics);
    _finalMetrics.onComplete();
    _finalMetrics.setComplete(true);
}

bool
Computer::findAlternativeSegmentFrom(SegmentStart *segment) {
    int semanticDistanceExplored = segment->getSemanticDistanceExplored();
    int previousI = -1;
    int previousJ = segment->getPreviousJ();
    bool hasOpenSequence = false;
    bool isFirst = true;
    for (uint32_t i = segment->getStartI(); i < getNumQueryTerms(); i++) {
        int semanticDistance = findClosestInFieldBySemanticDistance(i, previousJ, semanticDistanceExplored);
        int j = semanticDistanceToFieldIndex(semanticDistance, previousJ);

        if (j == -1 && semanticDistanceExplored > 0 && isFirst) {
            return false; // segment explored before; no more matches found
        }
        if (hasOpenSequence && (j == -1 || j != previousJ + 1)) {
            _currentMetrics.onSequenceEnd(previousJ);
            hasOpenSequence = false;
        }
        if (isFirst) {
            if (j != -1) {
                segmentStart(i, j, isFirst ? -1 : previousJ);
                segment->exploredTo(j);
                isFirst = false;
            }
            else {
                segment->incrementStartI(); // there are no matches for this i
            }
        }
        else {
            if ((unsigned int)std::abs(j - previousJ) >= _params.getProximityLimit()) {
                segmentEnd(i - 1, previousJ);
                return true;
            }
            else if (j != -1) {
                inSegment(i, j, previousJ, previousI);
            }
        }
        if (j != -1) {
            _currentMetrics.onMatch(i);
            if (!hasOpenSequence) {
                _currentMetrics.onSequenceStart(j);
                hasOpenSequence=true;
            }
            semanticDistanceExplored = 1; // skip the current match when looking for the next
        } else {
            semanticDistanceExplored = 0;
            // we have a match for this term but no position information
            if (_queryTermFieldMatch[i] != nullptr && !_cachedHits[i].valid) {
                _currentMetrics.onMatch(i);
            }
        }
        if (j >= 0) {
            previousI = i;
            previousJ = j;
        }
    }
    if (hasOpenSequence) {
        _currentMetrics.onSequenceEnd(previousJ);
    }
    if (!isFirst) {
        segmentEnd(getNumQueryTerms() - 1, previousJ);
        return true;
    }
    else {
        return false;
    }
}

void
Computer::inSegment(int i, int j, int previousJ, int previousI)
{
    _currentMetrics.onPair(i, j, previousJ);
    if (j == previousJ + 1 && i == previousI + 1) {
        _currentMetrics.onInSequence(i, j, previousJ);
    }
    else {
        _currentMetrics.onInSegmentGap(i, j, previousJ);
    }
}

bool
Computer::segmentStart(int i, int j, int previousJ)
{
    _currentMetrics.onNewSegment(i, j, previousJ);
    if (previousJ >= 0) {
        _currentMetrics.onPair(i, j, previousJ);
    }
    return true;
}

void
Computer::segmentEnd(int i, int j)
{
    SegmentStart *startOfNext = _segments[i + 1].segment.get();
    if (!_segments[i + 1].valid) {
        startOfNext->reset(_currentMetrics, j, i + 1);
        _segments[i + 1].valid = true;
    }
    else {
        startOfNext->offerHistory(j, _currentMetrics);
    }
}

SegmentStart *
Computer::findOpenSegment(uint32_t startI) {
    for (uint32_t i = startI; i < _segments.size(); i++) {
        SegmentStart *startPoint = _segments[i].valid ? _segments[i].segment.get() : nullptr;
        if (startPoint == nullptr || !startPoint->isOpen()) {
            continue;
        }
        if (startPoint->getSemanticDistanceExplored() == 0) {
            return startPoint; // first attempt
        }
        if (_alternativeSegmentationsTried >= _params.getMaxAlternativeSegmentations()) {
            continue;
        }
        _alternativeSegmentationsTried++;
        return startPoint;
    }
    return nullptr;
}

SegmentStart *
Computer::findLastStartPoint()
{
    for (int i = _segments.size(); --i >= 0; ) {
        SegmentStart *startPoint = _segments[i].valid ? _segments[i].segment.get() : nullptr;
        if (startPoint != nullptr) {
            return startPoint;
        }
    }
    LOG(error, "findLastStartPoint() could not find any segment start. This should never happen!");
    return nullptr;
}

void
Computer::setOccurrenceCounts(Metrics &metrics)
{
    // Find all unique query terms.
    std::vector<uint32_t> uniqueTerms;
    std::set<uint32_t> firstOccs;
    for (uint32_t i = 0; i < _queryTermFieldMatch.size(); ++i) {
        const TermFieldMatchData *termFieldMatch = _queryTermFieldMatch[i];
        if (termFieldMatch == nullptr) {
            continue; // not for this match
        }
        FieldPositionsIterator it = termFieldMatch->getIterator();
        if (it.valid()) {
            if (firstOccs.find(it.getPosition()) == firstOccs.end()) {
                uniqueTerms.push_back(i);
                firstOccs.insert(it.getPosition());
            }
        }
    }

    // Commence occurence logic.
    std::vector<feature_t> weightedOccurrences;
    std::vector<feature_t> significantOccurrences;

    uint32_t divider = std::min(_fieldLength, (uint32_t)(_params.getMaxOccurrences() * uniqueTerms.size()));
    uint32_t maxOccurence = std::min(_fieldLength, _params.getMaxOccurrences());

    feature_t occurrence = 0;
    feature_t absoluteOccurrence = 0;
    feature_t weightedAbsoluteOccurrence = 0;
    int totalWeight = 0;
    feature_t totalWeightedOccurrences = 0;
    feature_t totalSignificantOccurrences = 0;

    for (uint32_t termIdx : uniqueTerms) {
        const QueryTerm &queryTerm = _queryTerms[termIdx];
        const ITermData &termData = *queryTerm.termData();
        const TermFieldMatchData &termFieldMatch = *_queryTermFieldMatch[termIdx];

        uint32_t termOccurrences = 0;
        FieldPositionsIterator pos = termFieldMatch.getIterator();
        while (pos.valid() && termOccurrences < _params.getMaxOccurrences()) {
            termOccurrences++;
            pos.next();
        }

        occurrence += (feature_t)termOccurrences / divider;
        absoluteOccurrence += (feature_t)termOccurrences / (_params.getMaxOccurrences() * uniqueTerms.size());

        weightedAbsoluteOccurrence += (feature_t)termOccurrences * termData.getWeight().percent() / _params.getMaxOccurrences();
        totalWeight += termData.getWeight().percent();

        totalWeightedOccurrences += (feature_t)maxOccurence * termData.getWeight().percent() / divider;
        weightedOccurrences.push_back((feature_t)termOccurrences * termData.getWeight().percent() / divider);

        totalSignificantOccurrences += (feature_t)maxOccurence * queryTerm.significance() / divider;
        significantOccurrences.push_back((feature_t)termOccurrences * queryTerm.significance() / divider);
    }
    metrics.setOccurrence(occurrence);
    metrics.setAbsoluteOccurrence(absoluteOccurrence);
    metrics.setWeightedAbsoluteOccurrence(weightedAbsoluteOccurrence / (totalWeight > 0 ? totalWeight : 1));

    feature_t weightedOccurrenceSum = 0;
    for (feature_t feature : weightedOccurrences) {
        weightedOccurrenceSum += totalWeightedOccurrences > 0.0f ? feature / totalWeightedOccurrences : 0.0f;
    }
    metrics.setWeightedOccurrence(weightedOccurrenceSum);

    feature_t significantOccurrenceSum = 0;
    for (feature_t feature : significantOccurrences) {
        significantOccurrenceSum += totalSignificantOccurrences > 0.0f ? feature / totalSignificantOccurrences : 0.0f;
    }
    metrics.setSignificantOccurrence(significantOccurrenceSum);
}

}