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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "weak_and_search.h"
#include "wand_parts.h"
#include <vespa/searchlib/queryeval/orsearch.h>
#include <vespa/vespalib/util/left_right_heap.h>
#include <vespa/vespalib/util/priority_queue.h>
#include <vespa/vespalib/objects/visit.hpp>
namespace search::queryeval {
namespace wand {
template <typename FutureHeap, typename PastHeap, bool IS_STRICT>
class WeakAndSearchLR final : public WeakAndSearch
{
private:
using Scores = vespalib::PriorityQueue<score_t>;
VectorizedIteratorTerms _terms;
DualHeap<FutureHeap, PastHeap> _heaps;
Algorithm _algo;
score_t _threshold; // current score threshold
Scores _scores; // best n scores
const uint32_t _n;
void seek_strict(uint32_t docid) {
_algo.set_candidate(_terms, _heaps, docid);
if (_algo.solve_wand_constraint(_terms, _heaps, GreaterThanEqual(_threshold))) {
setDocId(_algo.get_candidate());
} else {
setAtEnd();
}
}
void seek_unstrict(uint32_t docid) {
if (docid > _algo.get_candidate()) {
_algo.set_candidate(_terms, _heaps, docid);
if (_algo.check_wand_constraint(_terms, _heaps, GreaterThanEqual(_threshold))) {
setDocId(_algo.get_candidate());
}
}
}
public:
WeakAndSearchLR(const Terms &terms, uint32_t n)
: _terms(terms, TermFrequencyScorer(), 0, {}),
_heaps(DocIdOrder(_terms.docId()), _terms.size()),
_algo(),
_threshold(1),
_scores(),
_n(n)
{
}
size_t get_num_terms() const override { return _terms.size(); }
int32_t get_term_weight(size_t idx) const override { return _terms.weight(idx); }
score_t get_max_score(size_t idx) const override { return _terms.maxScore(idx); }
const Terms &getTerms() const override { return _terms.input_terms(); }
uint32_t getN() const override { return _n; }
void doSeek(uint32_t docid) override {
if (IS_STRICT) {
seek_strict(docid);
} else {
seek_unstrict(docid);
}
}
void doUnpack(uint32_t docid) override {
_algo.find_matching_terms(_terms, _heaps);
_scores.push(_algo.get_upper_bound());
if (_scores.size() > _n) {
_scores.pop_front();
}
if (_scores.size() == _n) {
_threshold = _scores.front();
}
ref_t *end = _heaps.present_end();
for (ref_t *ref = _heaps.present_begin(); ref != end; ++ref) {
_terms.unpack(*ref, docid);
}
}
void initRange(uint32_t begin, uint32_t end) override {
WeakAndSearch::initRange(begin, end);
_algo.init_range(_terms, _heaps, begin, end);
if (_n == 0) {
setAtEnd();
}
}
Trinary is_strict() const override { return IS_STRICT ? Trinary::True : Trinary::False; }
};
//-----------------------------------------------------------------------------
} // namespace search::queryeval::wand
//-----------------------------------------------------------------------------
void
WeakAndSearch::visitMembers(vespalib::ObjectVisitor &visitor) const
{
visit(visitor, "n", getN());
visit(visitor, "terms", getTerms());
}
//-----------------------------------------------------------------------------
SearchIterator::UP
WeakAndSearch::createArrayWand(const Terms &terms, uint32_t n, bool strict)
{
if (strict) {
return std::make_unique<wand::WeakAndSearchLR<vespalib::LeftArrayHeap, vespalib::RightArrayHeap, true>>(terms, n);
} else {
return std::make_unique<wand::WeakAndSearchLR<vespalib::LeftArrayHeap, vespalib::RightArrayHeap, false>>(terms, n);
}
}
SearchIterator::UP
WeakAndSearch::createHeapWand(const Terms &terms, uint32_t n, bool strict)
{
if (strict) {
return std::make_unique<wand::WeakAndSearchLR<vespalib::LeftHeap, vespalib::RightHeap, true>>(terms, n);
} else {
return std::make_unique<wand::WeakAndSearchLR<vespalib::LeftHeap, vespalib::RightHeap, false>>(terms, n);
}
}
SearchIterator::UP
WeakAndSearch::create(const Terms &terms, uint32_t n, bool strict)
{
if (terms.size() < 128) {
return createArrayWand(terms, n, strict);
} else {
return createHeapWand(terms, n, strict);
}
}
//-----------------------------------------------------------------------------
}
|