diff --git a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapper.java b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapper.java index b57b29b21152a..f77c5788dc470 100644 --- a/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapper.java +++ b/modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapper.java @@ -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 { diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapperTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapperTests.java index 5f5a242e545a5..eea37e8faabca 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapperTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldMapperTests.java @@ -26,18 +26,21 @@ 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; import org.apache.lucene.search.spans.FieldMaskingSpanQuery; import org.apache.lucene.search.spans.SpanNearQuery; import org.apache.lucene.search.spans.SpanTermQuery; +import org.elasticsearch.Version; import org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.IndexSettings; @@ -54,6 +57,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; @@ -74,6 +78,7 @@ import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.core.IsInstanceOf.instanceOf; +import static org.mockito.Mockito.when; public class SearchAsYouTypeFieldMapperTests extends MapperTestCase { @@ -534,6 +539,35 @@ 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); + when(qsc.indexVersionCreated()).thenReturn(Version.CURRENT); + 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 terms) { final BooleanQuery.Builder builder = new BooleanQuery.Builder(); for (int i = 0; i < terms.size() - 1; i++) { diff --git a/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperServiceTestCase.java b/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperServiceTestCase.java index 8bc577b6c5a72..5233a66188dbf 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperServiceTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperServiceTestCase.java @@ -401,6 +401,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()) );