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
This commit is contained in:
Alan Woodward 2019-05-31 13:14:18 +01:00
parent 61c6a26b31
commit d0da30e5f4
2 changed files with 8 additions and 2 deletions

View File

@ -96,7 +96,7 @@ public class IntervalBuilder {
// formulate a single term, boolean, or phrase. // formulate a single term, boolean, or phrase.
if (numTokens == 0) { if (numTokens == 0) {
return null; return NO_INTERVALS;
} else if (numTokens == 1) { } else if (numTokens == 1) {
// single term // single term
return analyzeTerm(stream); return analyzeTerm(stream);
@ -231,7 +231,7 @@ public class IntervalBuilder {
return clauses; return clauses;
} }
private static final IntervalsSource NO_INTERVALS = new IntervalsSource() { static final IntervalsSource NO_INTERVALS = new IntervalsSource() {
@Override @Override
public IntervalIterator intervals(String field, LeafReaderContext ctx) { public IntervalIterator intervals(String field, LeafReaderContext ctx) {

View File

@ -110,6 +110,12 @@ public class IntervalBuilderTests extends ESTestCase {
} }
public void testEmptyTokenStream() throws IOException {
CannedTokenStream ts = new CannedTokenStream();
IntervalsSource source = BUILDER.analyzeText(new CachingTokenFilter(ts), 0, true);
assertSame(IntervalBuilder.NO_INTERVALS, source);
}
public void testSimpleSynonyms() throws IOException { public void testSimpleSynonyms() throws IOException {
CannedTokenStream ts = new CannedTokenStream( CannedTokenStream ts = new CannedTokenStream(