Skip to content

Commit

Permalink
Return NO_INTERVALS rather than null from empty TokenStream (#42750)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
romseygeek committed May 31, 2019
1 parent 61c6a26 commit d0da30e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected IntervalsSource analyzeText(CachingTokenFilter stream, int maxGaps, bo
// formulate a single term, boolean, or phrase.

if (numTokens == 0) {
return null;
return NO_INTERVALS;
} else if (numTokens == 1) {
// single term
return analyzeTerm(stream);
Expand Down Expand Up @@ -231,7 +231,7 @@ protected List<IntervalsSource> analyzeGraph(TokenStream source) throws IOExcept
return clauses;
}

private static final IntervalsSource NO_INTERVALS = new IntervalsSource() {
static final IntervalsSource NO_INTERVALS = new IntervalsSource() {

@Override
public IntervalIterator intervals(String field, LeafReaderContext ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public void testPhraseWithStopword() throws IOException {

}

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 {

CannedTokenStream ts = new CannedTokenStream(
Expand Down

0 comments on commit d0da30e

Please sign in to comment.