Skip to content

Commit

Permalink
Fix UOE when building exists query for nested search-as-you-type field (
Browse files Browse the repository at this point in the history
#64630)

PrefixFieldType can use the default existsQuery() implementation.

Fixes #64609
  • Loading branch information
romseygeek authored Nov 5, 2020
1 parent 27c36bf commit 9543d3b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,6 @@ public String typeName() {
public String toString() {
return super.toString() + ",prefixChars=" + minChars + ":" + maxChars;
}

@Override
public Query existsQuery(QueryShardContext context) {
throw new UnsupportedOperationException();
}
}

static final class PrefixFieldMapper extends FieldMapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.IndexableFieldType;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.DisjunctionMaxQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.MultiPhraseQuery;
import org.apache.lucene.search.NormsFieldExistsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SynonymQuery;
import org.apache.lucene.search.TermQuery;
Expand All @@ -54,6 +56,7 @@
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.index.search.QueryStringQueryParser;
import org.elasticsearch.plugins.Plugin;

import java.io.IOException;
Expand Down Expand Up @@ -532,6 +535,34 @@ public void testMatchPhrase() throws IOException {
}
}

public void testNestedExistsQuery() throws IOException, ParseException {
MapperService ms = createMapperService(mapping(b -> {
b.startObject("foo");
{
b.field("type", "object");
b.startObject("properties");
{
b.startObject("bar");
{
b.field("type", "search_as_you_type");
}
b.endObject();
}
b.endObject();
}
b.endObject();
}));
QueryShardContext qsc = createQueryShardContext(ms);
QueryStringQueryParser parser = new QueryStringQueryParser(qsc, "f");
Query q = parser.parse("foo:*");
assertEquals(new ConstantScoreQuery(new BooleanQuery.Builder()
.add(new NormsFieldExistsQuery("foo.bar"), BooleanClause.Occur.SHOULD)
.add(new NormsFieldExistsQuery("foo.bar._3gram"), BooleanClause.Occur.SHOULD)
.add(new NormsFieldExistsQuery("foo.bar._2gram"), BooleanClause.Occur.SHOULD)
.add(new TermQuery(new Term("_field_names", "foo.bar._index_prefix")), BooleanClause.Occur.SHOULD)
.build()), q);
}

private static BooleanQuery buildBoolPrefixQuery(String shingleFieldName, String prefixFieldName, List<String> terms) {
final BooleanQuery.Builder builder = new BooleanQuery.Builder();
for (int i = 0; i < terms.size() - 1; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ protected QueryShardContext createQueryShardContext(MapperService mapperService)
.thenAnswer(inv -> mapperService.fieldType(inv.getArguments()[0].toString()) != null);
when(queryShardContext.getIndexAnalyzers()).thenReturn(mapperService.getIndexAnalyzers());
when(queryShardContext.getIndexSettings()).thenReturn(mapperService.getIndexSettings());
when(queryShardContext.getObjectMapper(anyString())).thenAnswer(
inv -> mapperService.getObjectMapper(inv.getArguments()[0].toString()));
when(queryShardContext.simpleMatchToIndexNames(anyObject())).thenAnswer(
inv -> mapperService.simpleMatchToFullName(inv.getArguments()[0].toString())
);
Expand Down

0 comments on commit 9543d3b

Please sign in to comment.