Skip to content

Commit b8a208c

Browse files
committed
getTerms
1 parent 24fb5ef commit b8a208c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

x-pack/plugin/logsdb/src/main/java/org/elasticsearch/xpack/logsdb/patternedtext/PatternedTextFieldType.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99

1010
import org.apache.lucene.analysis.Analyzer;
1111
import org.apache.lucene.analysis.TokenStream;
12+
import org.apache.lucene.index.IndexReader;
1213
import org.apache.lucene.index.LeafReaderContext;
14+
import org.apache.lucene.index.MultiTerms;
1315
import org.apache.lucene.index.Term;
16+
import org.apache.lucene.index.Terms;
17+
import org.apache.lucene.index.TermsEnum;
1418
import org.apache.lucene.queries.intervals.Intervals;
1519
import org.apache.lucene.queries.intervals.IntervalsSource;
1620
import org.apache.lucene.search.ConstantScoreQuery;
@@ -24,14 +28,20 @@
2428
import org.apache.lucene.search.TermQuery;
2529
import org.apache.lucene.util.BytesRef;
2630
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;
2735
import org.elasticsearch.common.CheckedIntFunction;
2836
import org.elasticsearch.common.lucene.Lucene;
37+
import org.elasticsearch.common.lucene.search.AutomatonQueries;
2938
import org.elasticsearch.common.unit.Fuzziness;
3039
import org.elasticsearch.index.fielddata.FieldDataContext;
3140
import org.elasticsearch.index.fielddata.IndexFieldData;
3241
import org.elasticsearch.index.fielddata.SourceValueFetcherSortedBinaryIndexFieldData;
3342
import org.elasticsearch.index.mapper.BlockDocValuesReader;
3443
import org.elasticsearch.index.mapper.BlockLoader;
44+
import org.elasticsearch.index.mapper.SearchAfterTermsEnum;
3545
import org.elasticsearch.index.mapper.SourceValueFetcher;
3646
import org.elasticsearch.index.mapper.StringFieldType;
3747
import org.elasticsearch.index.mapper.TextFieldMapper;
@@ -105,6 +115,32 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
105115
return SourceValueFetcher.toString(name(), context, format);
106116
}
107117

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+
108144
private IOFunction<LeafReaderContext, CheckedIntFunction<List<Object>, IOException>> getValueFetcherProvider(
109145
SearchExecutionContext searchExecutionContext
110146
) {

0 commit comments

Comments
 (0)