Skip to content

Commit fd5687b

Browse files
author
Christoph Büscher
committed
Fix potential NPE in FuzzyTermsEnum
Under certain circumstances SpanMultiTermQueryWrapper uses SpanBooleanQueryRewriteWithMaxClause as its rewrite method, which in turn tries to get a TermsEnum from the wrapped MultiTermQuery currently using a `null` AttributeSource. While queries TermsQuery or subclasses of AutomatonQuery ignore this argument, FuzzyQuery uses it to create a FuzzyTermsEnum which triggers an NPE when the AttributeSource is not provided. This PR fixes this by supplying an empty AttributeSource instead of a `null` value. Closes elastic#52894
1 parent 60a7360 commit fd5687b

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

server/src/main/java/org/elasticsearch/common/lucene/search/SpanBooleanQueryRewriteWithMaxClause.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.lucene.search.spans.SpanOrQuery;
3434
import org.apache.lucene.search.spans.SpanQuery;
3535
import org.apache.lucene.search.spans.SpanTermQuery;
36+
import org.apache.lucene.util.AttributeSource;
3637
import org.apache.lucene.util.BytesRef;
3738

3839
import java.io.IOException;
@@ -92,11 +93,12 @@ private Collection<SpanQuery> collectTerms(IndexReader reader, MultiTermQuery qu
9293
continue;
9394
}
9495

95-
final TermsEnum termsEnum = getTermsEnum(query, terms, null);
96+
final TermsEnum termsEnum = getTermsEnum(query, terms, new AttributeSource());
9697
assert termsEnum != null;
9798

98-
if (termsEnum == TermsEnum.EMPTY)
99+
if (termsEnum == TermsEnum.EMPTY) {
99100
continue;
101+
}
100102

101103
BytesRef bytes;
102104
while ((bytes = termsEnum.next()) != null) {

server/src/test/java/org/elasticsearch/search/query/SearchQueryIT.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package org.elasticsearch.search.query;
2121

22+
import org.apache.lucene.index.IndexReader;
23+
import org.apache.lucene.search.MultiTermQuery;
2224
import org.apache.lucene.search.join.ScoreMode;
2325
import org.apache.lucene.util.English;
2426
import org.elasticsearch.action.index.IndexRequestBuilder;
@@ -28,6 +30,7 @@
2830
import org.elasticsearch.action.search.ShardSearchFailure;
2931
import org.elasticsearch.bootstrap.JavaVersion;
3032
import org.elasticsearch.common.document.DocumentField;
33+
import org.elasticsearch.common.lucene.search.SpanBooleanQueryRewriteWithMaxClause;
3134
import org.elasticsearch.common.settings.Settings;
3235
import org.elasticsearch.common.time.DateFormatter;
3336
import org.elasticsearch.common.unit.Fuzziness;
@@ -1758,4 +1761,19 @@ public void testFieldAliasesForMetaFields() throws Exception {
17581761
}
17591762

17601763
}
1764+
1765+
/**
1766+
* Test fix for NPE from {@link SpanBooleanQueryRewriteWithMaxClause#rewrite(IndexReader, MultiTermQuery)}.
1767+
* See https://github.com/elastic/elasticsearch/issues/52894 for details
1768+
*/
1769+
public void testIssue52894() {
1770+
createIndex("test");
1771+
client().prepareIndex("test").setId("1").setSource("field", "foobarbaz").get();
1772+
ensureGreen();
1773+
refresh();
1774+
1775+
BoolQueryBuilder query = boolQuery().filter(spanMultiTermQueryBuilder(fuzzyQuery("field", "foobarbiz").rewrite("constant_score")));
1776+
SearchResponse response = client().prepareSearch("test").setQuery(query).get();
1777+
assertHitCount(response, 1);
1778+
}
17611779
}

0 commit comments

Comments
 (0)