forked from jason-wolfe/search-index-benchmark-game
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathDoQuery.java
More file actions
79 lines (77 loc) · 3.82 KB
/
DoQuery.java
File metadata and controls
79 lines (77 loc) · 3.82 KB
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
import org.apache.lucene.analysis.CharArraySet;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.search.similarities.BM25Similarity;
import org.apache.lucene.store.FSDirectory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
public class DoQuery {
public static void main(String[] args) throws IOException, ParseException {
final Path indexDir = Paths.get(args[0]);
try (IndexReader reader = DirectoryReader.open(FSDirectory.open(indexDir))) {
final IndexSearcher searcher = new IndexSearcher(reader);
searcher.setQueryCache(null);
searcher.setSimilarity(new BM25Similarity(0.9f, 0.4f));
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in))) {
final QueryParser queryParser = new QueryParser("text", new StandardAnalyzer(CharArraySet.EMPTY_SET));
String line;
while ((line = bufferedReader.readLine()) != null) {
final String[] fields = line.trim().split("\t");
assert fields.length == 2;
final String command = fields[0];
final String query_str = fields[1];
Query query = queryParser
.parse(query_str)
.rewrite(reader);
final int count;
final TotalHitCountCollector countCollector = new TotalHitCountCollector();
switch (command) {
case "COUNT":
case "UNOPTIMIZED_COUNT":
count = searcher.count(query);
break;
case "TOP_10":
{
final TopDocs topDocs = searcher.search(query, 10);
count = 1;
}
break;
case "TOP_10_COUNT":
{
final TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(10, Integer.MAX_VALUE);
searcher.search(query, topScoreDocCollector);
count = topScoreDocCollector.getTotalHits();
}
break;
case "TOP_100_COUNT":
{
final TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(100, Integer.MAX_VALUE);
searcher.search(query, topScoreDocCollector);
count = topScoreDocCollector.getTotalHits();
}
break;
case "TOP_1000_COUNT":
{
final TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(1000, Integer.MAX_VALUE);
searcher.search(query, topScoreDocCollector);
count = topScoreDocCollector.getTotalHits();
}
break;
default:
System.out.println("UNSUPPORTED");
count = 0;
break;
}
System.out.println(count);
}
}
}
}
}