|
18 | 18 | */ |
19 | 19 | package org.elasticsearch.percolator; |
20 | 20 |
|
| 21 | +import org.apache.lucene.analysis.core.WhitespaceAnalyzer; |
| 22 | +import org.apache.lucene.document.Document; |
| 23 | +import org.apache.lucene.index.DirectoryReader; |
| 24 | +import org.apache.lucene.index.RandomIndexWriter; |
| 25 | +import org.apache.lucene.index.Term; |
| 26 | +import org.apache.lucene.index.memory.MemoryIndex; |
| 27 | +import org.apache.lucene.search.IndexSearcher; |
| 28 | +import org.apache.lucene.search.MatchAllDocsQuery; |
| 29 | +import org.apache.lucene.search.MatchNoDocsQuery; |
21 | 30 | import org.apache.lucene.search.ScoreDoc; |
| 31 | +import org.apache.lucene.search.TermQuery; |
22 | 32 | import org.apache.lucene.search.TopDocs; |
| 33 | +import org.apache.lucene.store.Directory; |
23 | 34 | import org.apache.lucene.util.FixedBitSet; |
| 35 | +import org.elasticsearch.search.SearchHit; |
24 | 36 | import org.elasticsearch.test.ESTestCase; |
25 | 37 |
|
| 38 | +import java.util.Collections; |
26 | 39 | import java.util.stream.IntStream; |
27 | 40 |
|
28 | 41 | public class PercolatorMatchedSlotSubFetchPhaseTests extends ESTestCase { |
29 | 42 |
|
| 43 | + public void testHitsExecute() throws Exception { |
| 44 | + try (Directory directory = newDirectory()) { |
| 45 | + // Need a one doc index: |
| 46 | + try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { |
| 47 | + Document document = new Document(); |
| 48 | + indexWriter.addDocument(document); |
| 49 | + } |
| 50 | + |
| 51 | + try (DirectoryReader reader = DirectoryReader.open(directory)) { |
| 52 | + IndexSearcher indexSearcher = new IndexSearcher(reader); |
| 53 | + |
| 54 | + // A match: |
| 55 | + { |
| 56 | + SearchHit[] hits = new SearchHit[]{new SearchHit(0)}; |
| 57 | + PercolateQuery.QueryStore queryStore = ctx -> docId -> new TermQuery(new Term("field", "value")); |
| 58 | + MemoryIndex memoryIndex = new MemoryIndex(); |
| 59 | + memoryIndex.addField("field", "value", new WhitespaceAnalyzer()); |
| 60 | + PercolateQuery percolateQuery = new PercolateQuery("_name", queryStore, Collections.emptyList(), |
| 61 | + new MatchAllDocsQuery(), memoryIndex.createSearcher(), new MatchNoDocsQuery()); |
| 62 | + |
| 63 | + PercolatorMatchedSlotSubFetchPhase.innerHitsExecute(percolateQuery, indexSearcher, hits); |
| 64 | + assertNotNull(hits[0].field(PercolatorMatchedSlotSubFetchPhase.FIELD_NAME_PREFIX)); |
| 65 | + assertEquals(0, (int) hits[0].field(PercolatorMatchedSlotSubFetchPhase.FIELD_NAME_PREFIX).getValue()); |
| 66 | + } |
| 67 | + |
| 68 | + // No match: |
| 69 | + { |
| 70 | + SearchHit[] hits = new SearchHit[]{new SearchHit(0)}; |
| 71 | + PercolateQuery.QueryStore queryStore = ctx -> docId -> new TermQuery(new Term("field", "value")); |
| 72 | + MemoryIndex memoryIndex = new MemoryIndex(); |
| 73 | + memoryIndex.addField("field", "value1", new WhitespaceAnalyzer()); |
| 74 | + PercolateQuery percolateQuery = new PercolateQuery("_name", queryStore, Collections.emptyList(), |
| 75 | + new MatchAllDocsQuery(), memoryIndex.createSearcher(), new MatchNoDocsQuery()); |
| 76 | + |
| 77 | + PercolatorMatchedSlotSubFetchPhase.innerHitsExecute(percolateQuery, indexSearcher, hits); |
| 78 | + assertNull(hits[0].field(PercolatorMatchedSlotSubFetchPhase.FIELD_NAME_PREFIX)); |
| 79 | + } |
| 80 | + |
| 81 | + // No query: |
| 82 | + { |
| 83 | + SearchHit[] hits = new SearchHit[]{new SearchHit(0)}; |
| 84 | + PercolateQuery.QueryStore queryStore = ctx -> docId -> null; |
| 85 | + MemoryIndex memoryIndex = new MemoryIndex(); |
| 86 | + memoryIndex.addField("field", "value", new WhitespaceAnalyzer()); |
| 87 | + PercolateQuery percolateQuery = new PercolateQuery("_name", queryStore, Collections.emptyList(), |
| 88 | + new MatchAllDocsQuery(), memoryIndex.createSearcher(), new MatchNoDocsQuery()); |
| 89 | + |
| 90 | + PercolatorMatchedSlotSubFetchPhase.innerHitsExecute(percolateQuery, indexSearcher, hits); |
| 91 | + assertNull(hits[0].field(PercolatorMatchedSlotSubFetchPhase.FIELD_NAME_PREFIX)); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
30 | 97 | public void testConvertTopDocsToSlots() { |
31 | 98 | ScoreDoc[] scoreDocs = new ScoreDoc[randomInt(128)]; |
32 | 99 | for (int i = 0; i < scoreDocs.length; i++) { |
|
0 commit comments