From f4ff5fe93b43a6593d0de71acb1a606cff22c186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Fri, 17 Jul 2020 17:23:23 +0200 Subject: [PATCH] Add `zero_terms_query` support to `match_phrase_prefix` (#58822) (#59784) Currently `match_phrase_prefix` doesn't support `zero_terms_query` like the other match-type queries. This change adds this support. Closes #58468 --- .../query/MatchPhrasePrefixQueryBuilder.java | 47 ++++++++++++++++++- .../MatchPhrasePrefixQueryBuilderTests.java | 24 ++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java index 8c24c3eea7a..0fe41a94ddc 100644 --- a/server/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java @@ -21,6 +21,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.FuzzyQuery; import org.apache.lucene.search.Query; +import org.elasticsearch.Version; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.StreamInput; @@ -28,6 +29,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.search.MatchQuery; +import org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery; import java.io.IOException; import java.util.Objects; @@ -39,6 +41,7 @@ import java.util.Objects; public class MatchPhrasePrefixQueryBuilder extends AbstractQueryBuilder { public static final String NAME = "match_phrase_prefix"; public static final ParseField MAX_EXPANSIONS_FIELD = new ParseField("max_expansions"); + public static final ParseField ZERO_TERMS_QUERY_FIELD = new ParseField("zero_terms_query"); private final String fieldName; @@ -50,6 +53,8 @@ public class MatchPhrasePrefixQueryBuilder extends AbstractQueryBuilder { @@ -64,6 +67,9 @@ public class MatchPhrasePrefixQueryBuilderTests extends AbstractQueryTestCase matchQuery.doToQuery(createShardContext())); } + public void testPhrasePrefixZeroTermsQuery() throws IOException { + MatchPhrasePrefixQueryBuilder matchQuery = new MatchPhrasePrefixQueryBuilder(TEXT_FIELD_NAME, ""); + matchQuery.zeroTermsQuery(ZeroTermsQuery.NONE); + assertEquals(new MatchNoDocsQuery(), matchQuery.doToQuery(createShardContext())); + + matchQuery = new MatchPhrasePrefixQueryBuilder(TEXT_FIELD_NAME, ""); + matchQuery.zeroTermsQuery(ZeroTermsQuery.ALL); + assertEquals(new MatchAllDocsQuery(), matchQuery.doToQuery(createShardContext())); + } + public void testPhrasePrefixMatchQuery() throws IOException { String json1 = "{\n" + " \"match_phrase_prefix\" : {\n" + @@ -128,6 +150,7 @@ public class MatchPhrasePrefixQueryBuilderTests extends AbstractQueryTestCase