|
22 | 22 | import org.apache.lucene.index.IndexReader;
|
23 | 23 | import org.apache.lucene.index.LeafReaderContext;
|
24 | 24 | import org.apache.lucene.index.Term;
|
25 |
| -import org.apache.lucene.search.BooleanQuery; |
26 | 25 | import org.apache.lucene.search.DocIdSetIterator;
|
27 | 26 | import org.apache.lucene.search.Explanation;
|
28 | 27 | import org.apache.lucene.search.IndexSearcher;
|
29 | 28 | import org.apache.lucene.search.Query;
|
30 | 29 | import org.apache.lucene.search.Scorer;
|
31 |
| -import org.apache.lucene.search.TermQuery; |
32 | 30 | import org.apache.lucene.search.TopDocs;
|
33 | 31 | import org.apache.lucene.search.TwoPhaseIterator;
|
34 | 32 | import org.apache.lucene.search.Weight;
|
35 | 33 | import org.apache.lucene.util.Accountable;
|
36 | 34 | import org.apache.lucene.util.Bits;
|
37 | 35 | import org.elasticsearch.common.bytes.BytesReference;
|
38 | 36 | import org.elasticsearch.common.lucene.Lucene;
|
39 |
| -import org.elasticsearch.common.lucene.search.MatchNoDocsQuery; |
40 | 37 |
|
41 | 38 | import java.io.IOException;
|
42 | 39 | import java.util.Objects;
|
43 | 40 | import java.util.Set;
|
44 | 41 |
|
45 |
| -import static org.apache.lucene.search.BooleanClause.Occur.FILTER; |
46 |
| - |
47 |
| -public final class PercolateQuery extends Query implements Accountable { |
| 42 | +final class PercolateQuery extends Query implements Accountable { |
48 | 43 |
|
49 | 44 | // cost of matching the query against the document, arbitrary as it would be really complex to estimate
|
50 | 45 | public static final float MATCH_COST = 1000;
|
51 | 46 |
|
52 |
| - public static class Builder { |
53 |
| - |
54 |
| - private final String docType; |
55 |
| - private final QueryStore queryStore; |
56 |
| - private final BytesReference documentSource; |
57 |
| - private final IndexSearcher percolatorIndexSearcher; |
58 |
| - |
59 |
| - private Query queriesMetaDataQuery; |
60 |
| - private Query verifiedQueriesQuery = new MatchNoDocsQuery(""); |
61 |
| - private Query percolateTypeQuery; |
62 |
| - |
63 |
| - /** |
64 |
| - * @param docType The type of the document being percolated |
65 |
| - * @param queryStore The lookup holding all the percolator queries as Lucene queries. |
66 |
| - * @param documentSource The source of the document being percolated |
67 |
| - * @param percolatorIndexSearcher The index searcher on top of the in-memory index that holds the document being percolated |
68 |
| - */ |
69 |
| - public Builder(String docType, QueryStore queryStore, BytesReference documentSource, IndexSearcher percolatorIndexSearcher) { |
70 |
| - this.docType = Objects.requireNonNull(docType); |
71 |
| - this.queryStore = Objects.requireNonNull(queryStore); |
72 |
| - this.documentSource = Objects.requireNonNull(documentSource); |
73 |
| - this.percolatorIndexSearcher = Objects.requireNonNull(percolatorIndexSearcher); |
74 |
| - } |
75 |
| - |
76 |
| - /** |
77 |
| - * Optionally sets a query that reduces the number of queries to percolate based on extracted terms from |
78 |
| - * the document to be percolated. |
79 |
| - * @param extractedTermsFieldName The name of the field to get the extracted terms from |
80 |
| - * @param extractionResultField The field to indicate for a document whether query term extraction was complete, |
81 |
| - * partial or failed. If query extraction was complete, the MemoryIndex doesn't |
82 |
| - */ |
83 |
| - public void extractQueryTermsQuery(String extractedTermsFieldName, String extractionResultField) throws IOException { |
84 |
| - // We can only skip the MemoryIndex verification when percolating a single document. |
85 |
| - // When the document being percolated contains a nested object field then the MemoryIndex contains multiple |
86 |
| - // documents. In this case the term query that indicates whether memory index verification can be skipped |
87 |
| - // can incorrectly indicate that non nested queries would match, while their nested variants would not. |
88 |
| - if (percolatorIndexSearcher.getIndexReader().maxDoc() == 1) { |
89 |
| - this.verifiedQueriesQuery = new TermQuery(new Term(extractionResultField, ExtractQueryTermsService.EXTRACTION_COMPLETE)); |
90 |
| - } |
91 |
| - this.queriesMetaDataQuery = ExtractQueryTermsService.createQueryTermsQuery( |
92 |
| - percolatorIndexSearcher.getIndexReader(), extractedTermsFieldName, |
93 |
| - // include extractionResultField:failed, because docs with this term have no extractedTermsField |
94 |
| - // and otherwise we would fail to return these docs. Docs that failed query term extraction |
95 |
| - // always need to be verified by MemoryIndex: |
96 |
| - new Term(extractionResultField, ExtractQueryTermsService.EXTRACTION_FAILED) |
97 |
| - ); |
98 |
| - } |
99 |
| - |
100 |
| - /** |
101 |
| - * @param percolateTypeQuery A query that identifies all document containing percolator queries |
102 |
| - */ |
103 |
| - public void setPercolateTypeQuery(Query percolateTypeQuery) { |
104 |
| - this.percolateTypeQuery = Objects.requireNonNull(percolateTypeQuery); |
105 |
| - } |
106 |
| - |
107 |
| - public PercolateQuery build() { |
108 |
| - if (percolateTypeQuery != null && queriesMetaDataQuery != null) { |
109 |
| - throw new IllegalStateException("Either filter by deprecated percolator type or by query metadata"); |
110 |
| - } |
111 |
| - // The query that selects which percolator queries will be evaluated by MemoryIndex: |
112 |
| - BooleanQuery.Builder queriesQuery = new BooleanQuery.Builder(); |
113 |
| - if (percolateTypeQuery != null) { |
114 |
| - queriesQuery.add(percolateTypeQuery, FILTER); |
115 |
| - } |
116 |
| - if (queriesMetaDataQuery != null) { |
117 |
| - queriesQuery.add(queriesMetaDataQuery, FILTER); |
118 |
| - } |
119 |
| - return new PercolateQuery(docType, queryStore, documentSource, queriesQuery.build(), percolatorIndexSearcher, |
120 |
| - verifiedQueriesQuery); |
121 |
| - } |
122 |
| - |
123 |
| - } |
124 |
| - |
125 | 47 | private final String documentType;
|
126 | 48 | private final QueryStore queryStore;
|
127 | 49 | private final BytesReference documentSource;
|
128 |
| - private final Query percolatorQueriesQuery; |
129 |
| - private final Query verifiedQueriesQuery; |
| 50 | + private final Query candidateMatchesQuery; |
| 51 | + private final Query verifiedMatchesQuery; |
130 | 52 | private final IndexSearcher percolatorIndexSearcher;
|
131 | 53 |
|
132 |
| - private PercolateQuery(String documentType, QueryStore queryStore, BytesReference documentSource, |
133 |
| - Query percolatorQueriesQuery, IndexSearcher percolatorIndexSearcher, Query verifiedQueriesQuery) { |
134 |
| - this.documentType = documentType; |
135 |
| - this.documentSource = documentSource; |
136 |
| - this.percolatorQueriesQuery = percolatorQueriesQuery; |
137 |
| - this.queryStore = queryStore; |
138 |
| - this.percolatorIndexSearcher = percolatorIndexSearcher; |
139 |
| - this.verifiedQueriesQuery = verifiedQueriesQuery; |
| 54 | + PercolateQuery(String documentType, QueryStore queryStore, BytesReference documentSource, |
| 55 | + Query candidateMatchesQuery, IndexSearcher percolatorIndexSearcher, Query verifiedMatchesQuery) { |
| 56 | + this.documentType = Objects.requireNonNull(documentType); |
| 57 | + this.documentSource = Objects.requireNonNull(documentSource); |
| 58 | + this.candidateMatchesQuery = Objects.requireNonNull(candidateMatchesQuery); |
| 59 | + this.queryStore = Objects.requireNonNull(queryStore); |
| 60 | + this.percolatorIndexSearcher = Objects.requireNonNull(percolatorIndexSearcher); |
| 61 | + this.verifiedMatchesQuery = Objects.requireNonNull(verifiedMatchesQuery); |
140 | 62 | }
|
141 | 63 |
|
142 | 64 | @Override
|
143 | 65 | public Query rewrite(IndexReader reader) throws IOException {
|
144 |
| - Query rewritten = percolatorQueriesQuery.rewrite(reader); |
145 |
| - if (rewritten != percolatorQueriesQuery) { |
| 66 | + Query rewritten = candidateMatchesQuery.rewrite(reader); |
| 67 | + if (rewritten != candidateMatchesQuery) { |
146 | 68 | return new PercolateQuery(documentType, queryStore, documentSource, rewritten, percolatorIndexSearcher,
|
147 |
| - verifiedQueriesQuery); |
| 69 | + verifiedMatchesQuery); |
148 | 70 | } else {
|
149 | 71 | return this;
|
150 | 72 | }
|
151 | 73 | }
|
152 | 74 |
|
153 | 75 | @Override
|
154 | 76 | public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
|
155 |
| - final Weight verifiedQueriesQueryWeight = verifiedQueriesQuery.createWeight(searcher, false); |
156 |
| - final Weight innerWeight = percolatorQueriesQuery.createWeight(searcher, needsScores); |
| 77 | + final Weight verifiedMatchesWeight = verifiedMatchesQuery.createWeight(searcher, false); |
| 78 | + final Weight candidateMatchesWeight = candidateMatchesQuery.createWeight(searcher, false); |
157 | 79 | return new Weight(this) {
|
158 | 80 | @Override
|
159 | 81 | public void extractTerms(Set<Term> set) {
|
@@ -183,17 +105,17 @@ public Explanation explain(LeafReaderContext leafReaderContext, int docId) throw
|
183 | 105 |
|
184 | 106 | @Override
|
185 | 107 | public float getValueForNormalization() throws IOException {
|
186 |
| - return innerWeight.getValueForNormalization(); |
| 108 | + return candidateMatchesWeight.getValueForNormalization(); |
187 | 109 | }
|
188 | 110 |
|
189 | 111 | @Override
|
190 | 112 | public void normalize(float v, float v1) {
|
191 |
| - innerWeight.normalize(v, v1); |
| 113 | + candidateMatchesWeight.normalize(v, v1); |
192 | 114 | }
|
193 | 115 |
|
194 | 116 | @Override
|
195 | 117 | public Scorer scorer(LeafReaderContext leafReaderContext) throws IOException {
|
196 |
| - final Scorer approximation = innerWeight.scorer(leafReaderContext); |
| 118 | + final Scorer approximation = candidateMatchesWeight.scorer(leafReaderContext); |
197 | 119 | if (approximation == null) {
|
198 | 120 | return null;
|
199 | 121 | }
|
@@ -226,7 +148,7 @@ public float score() throws IOException {
|
226 | 148 | }
|
227 | 149 | };
|
228 | 150 | } else {
|
229 |
| - Scorer verifiedDocsScorer = verifiedQueriesQueryWeight.scorer(leafReaderContext); |
| 151 | + Scorer verifiedDocsScorer = verifiedMatchesWeight.scorer(leafReaderContext); |
230 | 152 | Bits verifiedDocsBits = Lucene.asSequentialAccessBits(leafReaderContext.reader().maxDoc(), verifiedDocsScorer);
|
231 | 153 | return new BaseScorer(this, approximation, queries, percolatorIndexSearcher) {
|
232 | 154 |
|
@@ -293,7 +215,7 @@ public int hashCode() {
|
293 | 215 | @Override
|
294 | 216 | public String toString(String s) {
|
295 | 217 | return "PercolateQuery{document_type={" + documentType + "},document_source={" + documentSource.utf8ToString() +
|
296 |
| - "},inner={" + percolatorQueriesQuery.toString(s) + "}}"; |
| 218 | + "},inner={" + candidateMatchesQuery.toString(s) + "}}"; |
297 | 219 | }
|
298 | 220 |
|
299 | 221 | @Override
|
|
0 commit comments