|
21 | 21 | import org.apache.lucene.analysis.core.KeywordAnalyzer;
|
22 | 22 | import org.apache.lucene.document.Document;
|
23 | 23 | import org.apache.lucene.document.Field;
|
| 24 | +import org.apache.lucene.document.Field.Store; |
24 | 25 | import org.apache.lucene.document.SortedSetDocValuesField;
|
| 26 | +import org.apache.lucene.document.StringField; |
25 | 27 | import org.apache.lucene.index.DirectoryReader;
|
26 | 28 | import org.apache.lucene.index.IndexReader;
|
| 29 | +import org.apache.lucene.index.IndexWriter; |
27 | 30 | import org.apache.lucene.index.RandomIndexWriter;
|
| 31 | +import org.apache.lucene.index.Term; |
28 | 32 | import org.apache.lucene.queryparser.classic.QueryParser;
|
| 33 | +import org.apache.lucene.search.BooleanClause.Occur; |
| 34 | +import org.apache.lucene.search.BooleanQuery; |
29 | 35 | import org.apache.lucene.search.IndexSearcher;
|
30 | 36 | import org.apache.lucene.search.MatchAllDocsQuery;
|
31 | 37 | import org.apache.lucene.search.MatchNoDocsQuery;
|
32 | 38 | import org.apache.lucene.search.Query;
|
| 39 | +import org.apache.lucene.search.TermQuery; |
33 | 40 | import org.apache.lucene.store.Directory;
|
34 | 41 | import org.apache.lucene.util.BytesRef;
|
35 | 42 | import org.elasticsearch.index.mapper.KeywordFieldMapper;
|
|
39 | 46 | import org.elasticsearch.search.SearchHits;
|
40 | 47 | import org.elasticsearch.search.aggregations.Aggregation;
|
41 | 48 | import org.elasticsearch.search.aggregations.AggregationBuilder;
|
| 49 | +import org.elasticsearch.search.aggregations.AggregationBuilders; |
42 | 50 | import org.elasticsearch.search.aggregations.AggregatorTestCase;
|
43 | 51 | import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
44 | 52 | import org.elasticsearch.search.sort.SortOrder;
|
@@ -148,4 +156,47 @@ private Document document(String id, String... stringValues) {
|
148 | 156 | }
|
149 | 157 | return document;
|
150 | 158 | }
|
| 159 | + |
| 160 | + public void testSetScorer() throws Exception { |
| 161 | + Directory directory = newDirectory(); |
| 162 | + IndexWriter w = new IndexWriter(directory, newIndexWriterConfig() |
| 163 | + // only merge adjacent segments |
| 164 | + .setMergePolicy(newLogMergePolicy())); |
| 165 | + // first window (see BooleanScorer) has matches on one clause only |
| 166 | + for (int i = 0; i < 2048; ++i) { |
| 167 | + Document doc = new Document(); |
| 168 | + doc.add(new StringField("_id", Uid.encodeId(Integer.toString(i)), Store.YES)); |
| 169 | + if (i == 1000) { // any doc in 0..2048 |
| 170 | + doc.add(new StringField("string", "bar", Store.NO)); |
| 171 | + } |
| 172 | + w.addDocument(doc); |
| 173 | + } |
| 174 | + // second window has matches in two clauses |
| 175 | + for (int i = 0; i < 2048; ++i) { |
| 176 | + Document doc = new Document(); |
| 177 | + doc.add(new StringField("_id", Uid.encodeId(Integer.toString(2048 + i)), Store.YES)); |
| 178 | + if (i == 500) { // any doc in 0..2048 |
| 179 | + doc.add(new StringField("string", "baz", Store.NO)); |
| 180 | + } else if (i == 1500) { |
| 181 | + doc.add(new StringField("string", "bar", Store.NO)); |
| 182 | + } |
| 183 | + w.addDocument(doc); |
| 184 | + } |
| 185 | + |
| 186 | + w.forceMerge(1); // we need all docs to be in the same segment |
| 187 | + |
| 188 | + IndexReader reader = DirectoryReader.open(w); |
| 189 | + w.close(); |
| 190 | + |
| 191 | + IndexSearcher searcher = new IndexSearcher(reader); |
| 192 | + Query query = new BooleanQuery.Builder() |
| 193 | + .add(new TermQuery(new Term("string", "bar")), Occur.SHOULD) |
| 194 | + .add(new TermQuery(new Term("string", "baz")), Occur.SHOULD) |
| 195 | + .build(); |
| 196 | + AggregationBuilder agg = AggregationBuilders.topHits("top_hits"); |
| 197 | + TopHits result = searchAndReduce(searcher, query, agg, STRING_FIELD_TYPE); |
| 198 | + assertEquals(3, result.getHits().totalHits); |
| 199 | + reader.close(); |
| 200 | + directory.close(); |
| 201 | + } |
151 | 202 | }
|
0 commit comments