aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/queryeval/simple_phrase_search.cpp
blob: 431f386fa862f3617dab64dd94bd20b3b354b5d6 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "simple_phrase_search.h"
#include <vespa/searchlib/fef/termfieldmatchdata.h>
#include <vespa/vespalib/objects/visit.h>
#include <functional>
#include <cassert>

using search::fef::TermFieldMatchData;
using std::unique_ptr;
using std::transform;
using std::vector;
using vespalib::ObjectVisitor;

namespace search::queryeval {

namespace {
// Helper class
class PhraseMatcher {
    const fef::TermFieldMatchDataArray &_tmds;
    const vector<uint32_t> &_eval_order;
    vector<TermFieldMatchData::PositionsIterator> &_iterators;
    uint32_t _element_id;
    uint32_t _position;

    TermFieldMatchData::PositionsIterator &iterator(uint32_t word_index) {
        return _iterators[word_index];
    }

    TermFieldMatchData::PositionsIterator end(uint32_t word_index) {
        return _tmds[word_index]->end();
    }

    uint32_t elementId(uint32_t word_index) {
        return iterator(word_index)->getElementId();
    }

    uint32_t position(uint32_t word_index) {
        return iterator(word_index)->getPosition();
    }

    void iterateToElement(uint32_t word_index) {
        while (iterator(word_index) != end(word_index) &&
               elementId(word_index) < _element_id) {
            ++iterator(word_index);
        }
    }

    template <typename FwdIt>
    bool match(FwdIt first, FwdIt last) {
        if (first == last) {
            return true;
        }
        uint32_t word_index = *first;

        iterateToElement(word_index);
        while (iterator(word_index) != end(word_index) &&
               elementId(word_index) == _element_id) {
            if (position(word_index) == _position + word_index) {
                return match(++first, last);
            } else if (position(word_index) > _position + word_index) {
                return false;
            }
            ++iterator(word_index);
        }
        return false;
    }

    bool match() {
        _element_id = elementId(_eval_order[0]);
        if (position(_eval_order[0]) < _eval_order[0]) {
            // this position too early in element to allow match of other phrase terms
            return false;
        }
        _position = position(_eval_order[0]) - _eval_order[0];
        return match(++_eval_order.begin(), _eval_order.end());
    }

public:
    PhraseMatcher(const fef::TermFieldMatchDataArray &tmds,
                  const vector<uint32_t> &eval_order,
                  vector<TermFieldMatchData::PositionsIterator> &iterators)
        : _tmds(tmds),
          _eval_order(eval_order),
          _iterators(iterators),
          _element_id(0),
          _position(0)
    {
        for (size_t i = 0; i < _tmds.size(); ++i) {
            _iterators[i] = _tmds[i]->begin();
        }
    }

    bool hasMatch() {
        if (_tmds.size() == 1) {
            return true;
        }

        while (iterator(_eval_order[0]) != end(_eval_order[0])) {
            if (match()) {
                return true;
            }
            ++iterator(_eval_order[0]);
        }
        return false;
    }

    void fillPositions(TermFieldMatchData &tmd) {
        if (_tmds.size() == 1) {
            if (tmd.needs_normal_features()) {
                for (const fef::TermFieldMatchDataPosition & pos : *_tmds[0]) {
                    tmd.appendPosition(pos);
                }
            }
            if (tmd.needs_interleaved_features()) {
                tmd.setNumOccs(_tmds[0]->size());
                tmd.setFieldLength(_tmds[0]->getFieldLength());
            }
        } else {
            const bool needs_normal_features = tmd.needs_normal_features();
            uint32_t num_occs = 0;
            while (iterator(_eval_order[0]) != end(_eval_order[0])) {
                if (match()) {
                    if (needs_normal_features) {
                        tmd.appendPosition(*iterator(0));
                    }
                    ++num_occs;
                }
                ++iterator(_eval_order[0]);
            }
            if (tmd.needs_interleaved_features()) {
                tmd.setNumOccs(num_occs);
                tmd.setFieldLength(_tmds[0]->getFieldLength());
            }
        }
    }
};

bool
allTermsHaveMatch(const SimplePhraseSearch::Children &terms, const vector<uint32_t> &eval_order, uint32_t doc_id) {
    for (uint32_t i = 0; i < terms.size(); ++i) {
        if (!terms[eval_order[i]]->seek(doc_id)) {
            return false;
        }
    }
    return true;
}
}  // namespace

void
SimplePhraseSearch::phraseSeek(uint32_t doc_id) {
    if (allTermsHaveMatch(getChildren(), _eval_order, doc_id)) {
        AndSearch::doUnpack(doc_id);
        if (PhraseMatcher(_childMatch, _eval_order, _iterators).hasMatch()) {
            setDocId(doc_id);
        }
    }
}


SimplePhraseSearch::SimplePhraseSearch(Children children,
                                       fef::MatchData::UP md,
                                       fef::TermFieldMatchDataArray childMatch,
                                       vector<uint32_t> eval_order,
                                       TermFieldMatchData &tmd, bool strict)
    : AndSearch(std::move(children)),
      _md(std::move(md)),
      _childMatch(std::move(childMatch)),
      _eval_order(std::move(eval_order)),
      _tmd(tmd),
      _strict(strict),
      _iterators(getChildren().size())
{
    assert( ! getChildren().empty());
    assert(getChildren().size() == _childMatch.size());
    assert(getChildren().size() == _eval_order.size());
}

void
SimplePhraseSearch::doSeek(uint32_t doc_id) {
    phraseSeek(doc_id);
    if (_strict) {
        uint32_t next_candidate = doc_id;
        while (getDocId() < doc_id || getDocId() == beginId()) {
            getChildren()[0]->seek(next_candidate + 1);
            next_candidate = getChildren()[0]->getDocId();
            if (isAtEnd(next_candidate)) {
                setAtEnd();
                return;
            }
            // child must behave as strict.
            assert(next_candidate > doc_id && next_candidate != beginId());

            phraseSeek(next_candidate);
        }
    }
}

void
SimplePhraseSearch::doUnpack(uint32_t doc_id) {
    // All children has already been unpacked before this call is made.

    _tmd.reset(doc_id);
    PhraseMatcher(_childMatch, _eval_order, _iterators).fillPositions(_tmd);
}

void
SimplePhraseSearch::visitMembers(ObjectVisitor &visitor) const {
    AndSearch::visitMembers(visitor);
    visit(visitor, "strict", _strict);
}

}