|
9 | 9 |
|
10 | 10 | import org.apache.lucene.analysis.Analyzer; |
11 | 11 | import org.apache.lucene.analysis.TokenStream; |
| 12 | +import org.apache.lucene.index.IndexReader; |
12 | 13 | import org.apache.lucene.index.LeafReaderContext; |
| 14 | +import org.apache.lucene.index.MultiTerms; |
13 | 15 | import org.apache.lucene.index.Term; |
| 16 | +import org.apache.lucene.index.Terms; |
| 17 | +import org.apache.lucene.index.TermsEnum; |
14 | 18 | import org.apache.lucene.queries.intervals.Intervals; |
15 | 19 | import org.apache.lucene.queries.intervals.IntervalsSource; |
16 | 20 | import org.apache.lucene.search.ConstantScoreQuery; |
|
24 | 28 | import org.apache.lucene.search.TermQuery; |
25 | 29 | import org.apache.lucene.util.BytesRef; |
26 | 30 | import org.apache.lucene.util.IOFunction; |
| 31 | +import org.apache.lucene.util.automaton.Automata; |
| 32 | +import org.apache.lucene.util.automaton.Automaton; |
| 33 | +import org.apache.lucene.util.automaton.CompiledAutomaton; |
| 34 | +import org.apache.lucene.util.automaton.Operations; |
27 | 35 | import org.elasticsearch.common.CheckedIntFunction; |
28 | 36 | import org.elasticsearch.common.lucene.Lucene; |
| 37 | +import org.elasticsearch.common.lucene.search.AutomatonQueries; |
29 | 38 | import org.elasticsearch.common.unit.Fuzziness; |
30 | 39 | import org.elasticsearch.index.fielddata.FieldDataContext; |
31 | 40 | import org.elasticsearch.index.fielddata.IndexFieldData; |
32 | 41 | import org.elasticsearch.index.fielddata.SourceValueFetcherSortedBinaryIndexFieldData; |
33 | 42 | import org.elasticsearch.index.mapper.BlockDocValuesReader; |
34 | 43 | import org.elasticsearch.index.mapper.BlockLoader; |
| 44 | +import org.elasticsearch.index.mapper.SearchAfterTermsEnum; |
35 | 45 | import org.elasticsearch.index.mapper.SourceValueFetcher; |
36 | 46 | import org.elasticsearch.index.mapper.StringFieldType; |
37 | 47 | import org.elasticsearch.index.mapper.TextFieldMapper; |
@@ -105,6 +115,32 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format) |
105 | 115 | return SourceValueFetcher.toString(name(), context, format); |
106 | 116 | } |
107 | 117 |
|
| 118 | + @Override |
| 119 | + public TermsEnum getTerms(IndexReader reader, String prefix, boolean caseInsensitive, String searchAfter) throws IOException { |
| 120 | + Terms terms = MultiTerms.getTerms(reader, name()); |
| 121 | + if (terms == null) { |
| 122 | + // Field does not exist on this shard. |
| 123 | + return null; |
| 124 | + } |
| 125 | + Automaton a = caseInsensitive |
| 126 | + ? AutomatonQueries.caseInsensitivePrefix(prefix) |
| 127 | + : Operations.concatenate(Automata.makeString(prefix), Automata.makeAnyString()); |
| 128 | + assert a.isDeterministic(); |
| 129 | + |
| 130 | + CompiledAutomaton automaton = new CompiledAutomaton(a, true, true); |
| 131 | + |
| 132 | + BytesRef searchBytes = searchAfter == null ? null : new BytesRef(searchAfter); |
| 133 | + |
| 134 | + if (automaton.type == CompiledAutomaton.AUTOMATON_TYPE.ALL) { |
| 135 | + TermsEnum result = terms.iterator(); |
| 136 | + if (searchAfter != null) { |
| 137 | + result = new SearchAfterTermsEnum(result, searchBytes); |
| 138 | + } |
| 139 | + return result; |
| 140 | + } |
| 141 | + return terms.intersect(automaton, searchBytes); |
| 142 | + } |
| 143 | + |
108 | 144 | private IOFunction<LeafReaderContext, CheckedIntFunction<List<Object>, IOException>> getValueFetcherProvider( |
109 | 145 | SearchExecutionContext searchExecutionContext |
110 | 146 | ) { |
|
0 commit comments