Adding a null pointer check to fix index_prefix query (#2879)

* Adding a null pointer check to fix index_prefix query

Signed-off-by: Vacha Shah <vachshah@amazon.com>

* Adding test

Signed-off-by: Vacha Shah <vachshah@amazon.com>
This commit is contained in:
Vacha Shah 2022-04-14 11:07:22 -07:00 committed by GitHub
parent c4b684d3ae
commit 452e368bde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -583,7 +583,9 @@ public class TextFieldMapper extends ParametrizedFieldMapper {
}
Automaton automaton = Operations.concatenate(automata);
AutomatonQuery query = new AutomatonQuery(new Term(name(), value + "*"), automaton);
query.setRewriteMethod(method);
if (method != null) {
query.setRewriteMethod(method);
}
return new BooleanQuery.Builder().add(query, BooleanClause.Occur.SHOULD)
.add(new TermQuery(new Term(parentField.name(), value)), BooleanClause.Occur.SHOULD)
.build();

View File

@ -190,6 +190,17 @@ public class TextFieldTypeTests extends FieldTypeTestCase {
);
assertThat(q, equalTo(expected));
q = ft.prefixQuery("g", null, false, randomMockShardContext());
automaton = Operations.concatenate(Arrays.asList(Automata.makeChar('g'), Automata.makeAnyChar()));
expected = new ConstantScoreQuery(
new BooleanQuery.Builder().add(new AutomatonQuery(new Term("field._index_prefix", "g*"), automaton), BooleanClause.Occur.SHOULD)
.add(new TermQuery(new Term("field", "g")), BooleanClause.Occur.SHOULD)
.build()
);
assertThat(q, equalTo(expected));
}
public void testFetchSourceValue() throws IOException {