-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtantivy_search_benchmark.cpp
139 lines (123 loc) · 5.05 KB
/
tantivy_search_benchmark.cpp
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
#include <chrono>
#include <string>
#include <iostream>
#include <fstream>
#include "./src/store/MMapDirectory.hpp"
#include "./src/index/MMapDirectoryReader.hpp"
#include "./src/search/IndexSearcher.hpp"
#include "./src/search/SimpleTopScoreDocCollectorManager.hpp"
#include "./src/search/MatchCountCollectorManager.hpp"
#include "./src/search/query/TermQuery.hpp"
#include "./src/search/query/Query.hpp"
#include "./src/search/query/BooleanQuery.hpp"
#include "src/util/LinearAllocatorAdaptorForSTL.hpp"
using lucene::cyborg::index::LeafReaderContext;
using lucene::cyborg::index::MMapDirectoryReader;
using lucene::cyborg::index::MMapDirectoryReaderPtr;
using lucene::cyborg::index::PostingsEnum;
using lucene::cyborg::index::Terms;
using lucene::cyborg::search::Collector;
using lucene::cyborg::search::CollectorManager;
using lucene::cyborg::search::DocIdSetIterator;
using lucene::cyborg::search::IndexSearcher;
using lucene::cyborg::search::MatchCountCollectorManager;
using lucene::cyborg::search::Query;
using lucene::cyborg::search::ScoreDoc;
using lucene::cyborg::search::ScoreMode;
using lucene::cyborg::search::SearchContext;
using lucene::cyborg::search::SimpleTopScoreDocCollectorManager;
using lucene::cyborg::search::query::BooleanClause;
using lucene::cyborg::search::query::BooleanQuery;
using lucene::cyborg::search::query::BooleanQueryBuilder;
using lucene::cyborg::search::query::TermQuery;
using lucene::cyborg::store::MMapDirectory;
using lucene::cyborg::store::MMapDirectoryPtr;
using lucene::cyborg::util::LVector;
using lucene::cyborg::util::LinearAllocator;
using lucene::cyborg::util::LinearAllocatorAdapterForSTL;
using lucene::cyborg::util::ManagedPtr;
using lucene::cyborg::util::TypedVector;
using lucene::cyborg::util::UseOnlyPtr;
using std::chrono::high_resolution_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
using std::chrono::nanoseconds;
ManagedPtr<Query> parse_query_str(LinearAllocator &allocator,
const std::string_view &query_str) {
BooleanQueryBuilder builder{&allocator};
int j = 0;
for (int i = 0; i < query_str.size(); ++i) {
if (query_str[i] == ' ') {
if (query_str[j] == '+') {
builder.add_clause(BooleanClause{
allocator.allocate_object<TermQuery>(
"text", query_str.substr(j + 1, i - j - 1)), BooleanClause::Occur::MUST});
} else {
builder.add_clause(BooleanClause{
allocator.allocate_object<TermQuery>(
"text", query_str.substr(j, i - j)), BooleanClause::Occur::SHOULD});
}
j = i + 1;
}
}
if (query_str[j] == '+') {
builder.add_clause(BooleanClause{
allocator.allocate_object<TermQuery>(
"text", query_str.substr(j + 1, query_str.size() - j - 1)), BooleanClause::Occur::MUST});
} else {
builder.add_clause(BooleanClause{
allocator.allocate_object<TermQuery>(
"text", query_str.substr(j, query_str.size() - j)), BooleanClause::Occur::SHOULD});
}
return builder.build();
}
template<int N>
void top_search(IndexSearcher &searcher, const std::string_view &query_str) {
LinearAllocator allocator{};
SearchContext srch_context{&allocator};
SimpleTopScoreDocCollectorManager simple_top_score_doc_collector_manager{
&srch_context, ScoreMode::TOP_SCORES, N, N};
auto bool_q = parse_query_str(allocator, query_str);
searcher.search(&srch_context, bool_q, &simple_top_score_doc_collector_manager);
std::cout << 1 << '\n';
}
int count(IndexSearcher &searcher, const std::string_view &query_str) {
LinearAllocator allocator{};
SearchContext srch_context{&allocator};
MatchCountCollectorManager match_count_collector_manager{&srch_context};
auto bool_q = parse_query_str(allocator, query_str);
searcher.search(&srch_context, bool_q, &match_count_collector_manager);
return match_count_collector_manager.count;
}
int main(int argc, const char *argv[]) {
constexpr char DELIMITER = '\t';
std::string path = "./idx";
MMapDirectoryPtr dir = std::make_shared<MMapDirectory>(path);
auto index_reader = MMapDirectoryReader::open(dir);
IndexSearcher searcher{index_reader.get()};
std::string line;
while (std::getline(std::cin, line)) {
std::string_view read_line = line;
int i = 0;
for (int until = line.size(); i < until; ++i) {
if (line[i] == DELIMITER) {
break;
}
}
std::string_view command = read_line.substr(0, i);
std::string_view query_str = read_line.substr(i + 1, read_line.size() - i - 1);
if (command == "TOP_10") {
top_search<10>(searcher, query_str);
} else if (command == "TOP_100") {
top_search<100>(searcher, query_str);
} else if (command == "TOP_1000") {
top_search<1000>(searcher, query_str);
} else if (command == "COUNT") {
int cnt = count(searcher, query_str);
std::cout << cnt << '\n';
} else {
std::cout << "UNSUPPORTED\n";
}
} // End while:w
}