forked from ot/partitioned_elias_fano
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_log_runner.h
594 lines (491 loc) · 19.5 KB
/
query_log_runner.h
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#pragma once
#include <iostream>
#include <sstream>
#include "index_types.hpp"
#include "wand_data.hpp"
#include "util.hpp"
namespace quasi_succinct {
typedef uint32_t term_id_type;
typedef std::vector<term_id_type> term_id_vec;
bool read_query(term_id_vec& ret, std::istream& is = std::cin)
{
ret.clear();
std::string line;
if (!std::getline(is, line)) return false;
std::istringstream iline(line);
term_id_type term_id;
while (iline >> term_id) {
ret.push_back(term_id);
}
return true;
}
void remove_duplicate_terms(term_id_vec& terms)
{
std::sort(terms.begin(), terms.end());
terms.erase(std::unique(terms.begin(), terms.end()), terms.end());
}
template <bool with_freqs>
struct and_query {
template <typename Index>
uint64_t operator()(Index const& index, term_id_vec terms) const
{
if (terms.empty()) return 0;
remove_duplicate_terms(terms);
typedef typename Index::document_enumerator enum_type;
std::vector<enum_type> enums;
enums.reserve(terms.size());
for (auto term: terms) {
enums.push_back(index[term]);
}
// sort by increasing frequency
std::sort(enums.begin(), enums.end(),
[](enum_type const& lhs, enum_type const& rhs) {
return lhs.size() < rhs.size();
});
uint64_t results = 0;
uint64_t candidate = enums[0].docid();
size_t i = 1;
while (candidate < index.num_docs()) {
for (; i < enums.size(); ++i) {
enums[i].next_geq(candidate);
if (enums[i].docid() != candidate) {
candidate = enums[i].docid();
i = 0;
break;
}
}
if (i == enums.size()) {
results += 1;
if (with_freqs) {
for (i = 0; i < enums.size(); ++i) {
do_not_optimize_away(enums[i].freq());
}
}
enums[0].next();
candidate = enums[0].docid();
i = 1;
}
}
return results;
}
};
template <bool with_freqs>
struct or_query {
template <typename Index>
uint64_t operator()(Index const& index, term_id_vec terms) const
{
if (terms.empty()) return 0;
remove_duplicate_terms(terms);
typedef typename Index::document_enumerator enum_type;
std::vector<enum_type> enums;
enums.reserve(terms.size());
for (auto term: terms) {
enums.push_back(index[term]);
}
uint64_t results = 0;
uint64_t cur_doc = std::min_element(enums.begin(), enums.end(),
[](enum_type const& lhs, enum_type const& rhs) {
return lhs.docid() < rhs.docid();
})->docid();
while (cur_doc < index.num_docs()) {
results += 1;
uint64_t next_doc = index.num_docs();
for (size_t i = 0; i < enums.size(); ++i) {
if (enums[i].docid() == cur_doc) {
if (with_freqs) {
do_not_optimize_away(enums[i].freq());
}
enums[i].next();
}
if (enums[i].docid() < next_doc) {
next_doc = enums[i].docid();
}
}
cur_doc = next_doc;
}
return results;
}
};
typedef std::pair<uint64_t, uint64_t> term_freq_pair;
typedef std::vector<term_freq_pair> term_freq_vec;
term_freq_vec query_freqs(term_id_vec terms)
{
term_freq_vec query_term_freqs;
std::sort(terms.begin(), terms.end());
// count query term frequencies
for (size_t i = 0; i < terms.size(); ++i) {
if (i == 0 || terms[i] != terms[i - 1]) {
query_term_freqs.emplace_back(terms[i], 1);
} else {
query_term_freqs.back().second += 1;
}
}
return query_term_freqs;
}
struct topk_queue {
topk_queue(uint64_t k)
: m_k(k)
{}
bool insert(float score)
{
if (m_q.size() < m_k) {
m_q.push_back(score);
std::push_heap(m_q.begin(), m_q.end(), std::greater<float>());
return true;
} else {
if (score > m_q.front()) {
std::pop_heap(m_q.begin(), m_q.end(), std::greater<float>());
m_q.back() = score;
std::push_heap(m_q.begin(), m_q.end(), std::greater<float>());
return true;
}
}
return false;
}
bool would_enter(float score) const
{
return m_q.size() < m_k || score > m_q.front();
}
void finalize()
{
std::sort_heap(m_q.begin(), m_q.end(), std::greater<float>());
}
std::vector<float> const& topk() const
{
return m_q;
}
void clear()
{
m_q.clear();
}
private:
uint64_t m_k;
std::vector<float> m_q;
};
struct wand_query {
typedef bm25 scorer_type;
wand_query(wand_data<scorer_type> const& wdata, uint64_t k)
: m_wdata(wdata)
, m_topk(k)
{}
template <typename Index>
uint64_t operator()(Index const& index, term_id_vec const& terms)
{
m_topk.clear();
if (terms.empty()) return 0;
auto query_term_freqs = query_freqs(terms);
uint64_t num_docs = index.num_docs();
typedef typename Index::document_enumerator enum_type;
struct scored_enum {
enum_type docs_enum;
float q_weight;
float max_weight;
};
std::vector<scored_enum> enums;
enums.reserve(query_term_freqs.size());
for (auto term: query_term_freqs) {
auto list = index[term.first];
auto q_weight = scorer_type::query_term_weight
(term.second, list.size(), num_docs);
auto max_weight = q_weight * m_wdata.max_term_weight(term.first);
enums.push_back(scored_enum {std::move(list), q_weight, max_weight});
}
std::vector<scored_enum*> ordered_enums;
ordered_enums.reserve(enums.size());
for (auto& en: enums) {
ordered_enums.push_back(&en);
}
auto sort_enums = [&]() {
// sort enumerators by increasing docid
std::sort(ordered_enums.begin(), ordered_enums.end(),
[](scored_enum* lhs, scored_enum* rhs) {
return lhs->docs_enum.docid() < rhs->docs_enum.docid();
});
};
sort_enums();
while (true) {
// find pivot
float upper_bound = 0;
size_t pivot;
bool found_pivot = false;
for (pivot = 0; pivot < ordered_enums.size(); ++pivot) {
if (ordered_enums[pivot]->docs_enum.docid() == num_docs) {
break;
}
upper_bound += ordered_enums[pivot]->max_weight;
if (m_topk.would_enter(upper_bound)) {
found_pivot = true;
break;
}
}
// no pivot found, we can stop the search
if (!found_pivot) {
break;
}
// check if pivot is a possible match
uint64_t pivot_id = ordered_enums[pivot]->docs_enum.docid();
if (pivot_id == ordered_enums[0]->docs_enum.docid()) {
float score = 0;
float norm_len = m_wdata.norm_len(pivot_id);
for (scored_enum* en: ordered_enums) {
if (en->docs_enum.docid() != pivot_id) {
break;
}
score += en->q_weight * scorer_type::doc_term_weight
(en->docs_enum.freq(), norm_len);
en->docs_enum.next();
}
m_topk.insert(score);
// resort by docid
sort_enums();
} else {
// no match, move farthest list up to the pivot
uint64_t next_list = pivot;
for (; ordered_enums[next_list]->docs_enum.docid() == pivot_id;
--next_list);
ordered_enums[next_list]->docs_enum.next_geq(pivot_id);
// bubble down the advanced list
for (size_t i = next_list + 1; i < ordered_enums.size(); ++i) {
if (ordered_enums[i]->docs_enum.docid() <
ordered_enums[i - 1]->docs_enum.docid()) {
std::swap(ordered_enums[i], ordered_enums[i - 1]);
} else {
break;
}
}
}
}
m_topk.finalize();
return m_topk.topk().size();
}
std::vector<float> const& topk() const
{
return m_topk.topk();
}
private:
wand_data<scorer_type> const& m_wdata;
topk_queue m_topk;
};
struct ranked_and_query {
typedef bm25 scorer_type;
ranked_and_query(wand_data<scorer_type> const& wdata, uint64_t k)
: m_wdata(wdata)
, m_topk(k)
{}
template <typename Index>
uint64_t operator()(Index const& index, term_id_vec terms)
{
m_topk.clear();
if (terms.empty()) return 0;
auto query_term_freqs = query_freqs(terms);
uint64_t num_docs = index.num_docs();
typedef typename Index::document_enumerator enum_type;
struct scored_enum {
enum_type docs_enum;
float q_weight;
};
std::vector<scored_enum> enums;
enums.reserve(query_term_freqs.size());
for (auto term: query_term_freqs) {
auto list = index[term.first];
auto q_weight = scorer_type::query_term_weight
(term.second, list.size(), num_docs);
enums.push_back(scored_enum {std::move(list), q_weight});
}
// sort by increasing frequency
std::sort(enums.begin(), enums.end(),
[](scored_enum const& lhs, scored_enum const& rhs) {
return lhs.docs_enum.size() < rhs.docs_enum.size();
});
uint64_t candidate = enums[0].docs_enum.docid();
size_t i = 1;
while (candidate < index.num_docs()) {
for (; i < enums.size(); ++i) {
enums[i].docs_enum.next_geq(candidate);
if (enums[i].docs_enum.docid() != candidate) {
candidate = enums[i].docs_enum.docid();
i = 0;
break;
}
}
if (i == enums.size()) {
float norm_len = m_wdata.norm_len(candidate);
float score = 0;
for (i = 0; i < enums.size(); ++i) {
score += enums[i].q_weight * scorer_type::doc_term_weight
(enums[i].docs_enum.freq(), norm_len);
}
m_topk.insert(score);
enums[0].docs_enum.next();
candidate = enums[0].docs_enum.docid();
i = 1;
}
}
m_topk.finalize();
return m_topk.topk().size();
}
std::vector<float> const& topk() const
{
return m_topk.topk();
}
private:
wand_data<scorer_type> const& m_wdata;
topk_queue m_topk;
};
struct ranked_or_query {
typedef bm25 scorer_type;
ranked_or_query(wand_data<scorer_type> const& wdata, uint64_t k)
: m_wdata(wdata)
, m_topk(k)
{}
template <typename Index>
uint64_t operator()(Index const& index, term_id_vec terms)
{
m_topk.clear();
if (terms.empty()) return 0;
auto query_term_freqs = query_freqs(terms);
uint64_t num_docs = index.num_docs();
typedef typename Index::document_enumerator enum_type;
struct scored_enum {
enum_type docs_enum;
float q_weight;
};
std::vector<scored_enum> enums;
enums.reserve(query_term_freqs.size());
for (auto term: query_term_freqs) {
auto list = index[term.first];
auto q_weight = scorer_type::query_term_weight
(term.second, list.size(), num_docs);
enums.push_back(scored_enum {std::move(list), q_weight});
}
uint64_t cur_doc =
std::min_element(enums.begin(), enums.end(),
[](scored_enum const& lhs, scored_enum const& rhs) {
return lhs.docs_enum.docid() < rhs.docs_enum.docid();
})
->docs_enum.docid();
while (cur_doc < index.num_docs()) {
float score = 0;
float norm_len = m_wdata.norm_len(cur_doc);
uint64_t next_doc = index.num_docs();
for (size_t i = 0; i < enums.size(); ++i) {
if (enums[i].docs_enum.docid() == cur_doc) {
score += enums[i].q_weight * scorer_type::doc_term_weight
(enums[i].docs_enum.freq(), norm_len);
enums[i].docs_enum.next();
}
if (enums[i].docs_enum.docid() < next_doc) {
next_doc = enums[i].docs_enum.docid();
}
}
m_topk.insert(score);
cur_doc = next_doc;
}
m_topk.finalize();
return m_topk.topk().size();
}
std::vector<float> const& topk() const
{
return m_topk.topk();
}
private:
wand_data<scorer_type> const& m_wdata;
topk_queue m_topk;
};
struct maxscore_query {
typedef bm25 scorer_type;
maxscore_query(wand_data<scorer_type> const& wdata, uint64_t k)
: m_wdata(wdata)
, m_topk(k)
{}
template <typename Index>
uint64_t operator()(Index const& index, term_id_vec const& terms)
{
m_topk.clear();
if (terms.empty()) return 0;
auto query_term_freqs = query_freqs(terms);
uint64_t num_docs = index.num_docs();
typedef typename Index::document_enumerator enum_type;
struct scored_enum {
enum_type docs_enum;
float q_weight;
float max_weight;
};
std::vector<scored_enum> enums;
enums.reserve(query_term_freqs.size());
for (auto term: query_term_freqs) {
auto list = index[term.first];
auto q_weight = scorer_type::query_term_weight
(term.second, list.size(), num_docs);
auto max_weight = q_weight * m_wdata.max_term_weight(term.first);
enums.push_back(scored_enum {std::move(list), q_weight, max_weight});
}
std::vector<scored_enum*> ordered_enums;
ordered_enums.reserve(enums.size());
for (auto& en: enums) {
ordered_enums.push_back(&en);
}
// sort enumerators by increasing maxscore
std::sort(ordered_enums.begin(), ordered_enums.end(),
[](scored_enum* lhs, scored_enum* rhs) {
return lhs->max_weight < rhs->max_weight;
});
std::vector<float> upper_bounds(ordered_enums.size());
upper_bounds[0] = ordered_enums[0]->max_weight;
for (size_t i = 1; i < ordered_enums.size(); ++i) {
upper_bounds[i] = upper_bounds[i - 1] + ordered_enums[i]->max_weight;
}
uint64_t non_essential_lists = 0;
uint64_t cur_doc =
std::min_element(enums.begin(), enums.end(),
[](scored_enum const& lhs, scored_enum const& rhs) {
return lhs.docs_enum.docid() < rhs.docs_enum.docid();
})
->docs_enum.docid();
while (non_essential_lists < ordered_enums.size() &&
cur_doc < index.num_docs()) {
float score = 0;
float norm_len = m_wdata.norm_len(cur_doc);
uint64_t next_doc = index.num_docs();
for (size_t i = non_essential_lists; i < ordered_enums.size(); ++i) {
if (ordered_enums[i]->docs_enum.docid() == cur_doc) {
score += ordered_enums[i]->q_weight * scorer_type::doc_term_weight
(ordered_enums[i]->docs_enum.freq(), norm_len);
ordered_enums[i]->docs_enum.next();
}
if (ordered_enums[i]->docs_enum.docid() < next_doc) {
next_doc = ordered_enums[i]->docs_enum.docid();
}
}
// try to complete evaluation with non-essential lists
for (size_t i = non_essential_lists - 1; i + 1 > 0; --i) {
if (!m_topk.would_enter(score + upper_bounds[i])) {
break;
}
ordered_enums[i]->docs_enum.next_geq(cur_doc);
if (ordered_enums[i]->docs_enum.docid() == cur_doc) {
score += ordered_enums[i]->q_weight * scorer_type::doc_term_weight
(ordered_enums[i]->docs_enum.freq(), norm_len);
}
}
if (m_topk.insert(score)) {
// update non-essential lists
while (non_essential_lists < ordered_enums.size() &&
!m_topk.would_enter(upper_bounds[non_essential_lists])) {
non_essential_lists += 1;
}
}
cur_doc = next_doc;
}
m_topk.finalize();
return m_topk.topk().size();
}
std::vector<float> const& topk() const
{
return m_topk.topk();
}
private:
wand_data<scorer_type> const& m_wdata;
topk_queue m_topk;
};
}