Skip to content

Commit a3805dd

Browse files
committed
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 6d8909a commit a3805dd

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
@@ -95,7 +95,7 @@ protected IntervalsSource analyzeText(CachingTokenFilter stream, int maxGaps, bo
9595
// formulate a single term, boolean, or phrase.
9696

9797
if (numTokens == 0) {
98-
return null;
98+
return NO_INTERVALS;
9999
} else if (numTokens == 1) {
100100
// single term
101101
return analyzeTerm(stream);
@@ -230,7 +230,7 @@ protected List<IntervalsSource> analyzeGraph(TokenStream source) throws IOExcept
230230
return clauses;
231231
}
232232

233-
private static final IntervalsSource NO_INTERVALS = new IntervalsSource() {
233+
static final IntervalsSource NO_INTERVALS = new IntervalsSource() {
234234

235235
@Override
236236
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)