Skip to content

Commit a794736

Browse files
authored
Return NO_INTERVALS rather than null from empty TokenStream (#42750)
IntervalBuilder#analyzeText will currently return null if it is passed an empty TokenStream, which can lead to a confusing NullPointerException later on during querying. This commit changes the code to return NO_INTERVALS instead. Fixes #42587
1 parent 053e154 commit a794736

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

server/src/main/java/org/elasticsearch/index/query/IntervalBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected IntervalsSource analyzeText(CachingTokenFilter stream, int maxGaps, bo
9696
// formulate a single term, boolean, or phrase.
9797

9898
if (numTokens == 0) {
99-
return null;
99+
return NO_INTERVALS;
100100
} else if (numTokens == 1) {
101101
// single term
102102
return analyzeTerm(stream);
@@ -231,7 +231,7 @@ protected List<IntervalsSource> analyzeGraph(TokenStream source) throws IOExcept
231231
return clauses;
232232
}
233233

234-
private static final IntervalsSource NO_INTERVALS = new IntervalsSource() {
234+
static final IntervalsSource NO_INTERVALS = new IntervalsSource() {
235235

236236
@Override
237237
public IntervalIterator intervals(String field, LeafReaderContext ctx) {

server/src/test/java/org/elasticsearch/index/query/IntervalBuilderTests.java

+6
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ public void testPhraseWithStopword() throws IOException {
110110

111111
}
112112

113+
public void testEmptyTokenStream() throws IOException {
114+
CannedTokenStream ts = new CannedTokenStream();
115+
IntervalsSource source = BUILDER.analyzeText(new CachingTokenFilter(ts), 0, true);
116+
assertSame(IntervalBuilder.NO_INTERVALS, source);
117+
}
118+
113119
public void testSimpleSynonyms() throws IOException {
114120

115121
CannedTokenStream ts = new CannedTokenStream(

0 commit comments

Comments
 (0)