2020package org .elasticsearch .index .query ;
2121
2222import org .apache .lucene .search .Query ;
23+ import org .elasticsearch .Version ;
2324import org .elasticsearch .common .ParseField ;
2425import org .elasticsearch .common .ParsingException ;
2526import org .elasticsearch .common .Strings ;
2829import org .elasticsearch .common .xcontent .XContentBuilder ;
2930import org .elasticsearch .common .xcontent .XContentParser ;
3031import org .elasticsearch .index .search .MatchQuery ;
32+ import org .elasticsearch .index .search .MatchQuery .ZeroTermsQuery ;
3133
3234import java .io .IOException ;
3335import java .util .Objects ;
3941public class MatchPhraseQueryBuilder extends AbstractQueryBuilder <MatchPhraseQueryBuilder > {
4042 public static final String NAME = "match_phrase" ;
4143 public static final ParseField SLOP_FIELD = new ParseField ("slop" );
44+ public static final ParseField ZERO_TERMS_QUERY_FIELD = new ParseField ("zero_terms_query" );
4245
4346 private final String fieldName ;
4447
@@ -48,6 +51,8 @@ public class MatchPhraseQueryBuilder extends AbstractQueryBuilder<MatchPhraseQue
4851
4952 private int slop = MatchQuery .DEFAULT_PHRASE_SLOP ;
5053
54+ private ZeroTermsQuery zeroTermsQuery = MatchQuery .DEFAULT_ZERO_TERMS_QUERY ;
55+
5156 public MatchPhraseQueryBuilder (String fieldName , Object value ) {
5257 if (Strings .isEmpty (fieldName )) {
5358 throw new IllegalArgumentException ("[" + NAME + "] requires fieldName" );
@@ -67,6 +72,9 @@ public MatchPhraseQueryBuilder(StreamInput in) throws IOException {
6772 fieldName = in .readString ();
6873 value = in .readGenericValue ();
6974 slop = in .readVInt ();
75+ if (in .getVersion ().onOrAfter (Version .V_7_0_0_alpha1 )) {
76+ zeroTermsQuery = ZeroTermsQuery .readFromStream (in );
77+ }
7078 analyzer = in .readOptionalString ();
7179 }
7280
@@ -75,6 +83,9 @@ protected void doWriteTo(StreamOutput out) throws IOException {
7583 out .writeString (fieldName );
7684 out .writeGenericValue (value );
7785 out .writeVInt (slop );
86+ if (out .getVersion ().onOrAfter (Version .V_7_0_0_alpha1 )) {
87+ zeroTermsQuery .writeTo (out );
88+ }
7889 out .writeOptionalString (analyzer );
7990 }
8091
@@ -116,6 +127,23 @@ public int slop() {
116127 return this .slop ;
117128 }
118129
130+ /**
131+ * Sets query to use in case no query terms are available, e.g. after analysis removed them.
132+ * Defaults to {@link ZeroTermsQuery#NONE}, but can be set to
133+ * {@link ZeroTermsQuery#ALL} instead.
134+ */
135+ public MatchPhraseQueryBuilder zeroTermsQuery (ZeroTermsQuery zeroTermsQuery ) {
136+ if (zeroTermsQuery == null ) {
137+ throw new IllegalArgumentException ("[" + NAME + "] requires zeroTermsQuery to be non-null" );
138+ }
139+ this .zeroTermsQuery = zeroTermsQuery ;
140+ return this ;
141+ }
142+
143+ public ZeroTermsQuery zeroTermsQuery () {
144+ return this .zeroTermsQuery ;
145+ }
146+
119147 @ Override
120148 public String getWriteableName () {
121149 return NAME ;
@@ -131,6 +159,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
131159 builder .field (MatchQueryBuilder .ANALYZER_FIELD .getPreferredName (), analyzer );
132160 }
133161 builder .field (SLOP_FIELD .getPreferredName (), slop );
162+ builder .field (ZERO_TERMS_QUERY_FIELD .getPreferredName (), zeroTermsQuery .toString ());
134163 printBoostAndQueryName (builder );
135164 builder .endObject ();
136165 builder .endObject ();
@@ -148,14 +177,18 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
148177 matchQuery .setAnalyzer (analyzer );
149178 }
150179 matchQuery .setPhraseSlop (slop );
180+ matchQuery .setZeroTermsQuery (zeroTermsQuery );
151181
152182 return matchQuery .parse (MatchQuery .Type .PHRASE , fieldName , value );
153183 }
154184
155185 @ Override
156186 protected boolean doEquals (MatchPhraseQueryBuilder other ) {
157- return Objects .equals (fieldName , other .fieldName ) && Objects .equals (value , other .value ) && Objects .equals (analyzer , other .analyzer )
158- && Objects .equals (slop , other .slop );
187+ return Objects .equals (fieldName , other .fieldName )
188+ && Objects .equals (value , other .value )
189+ && Objects .equals (analyzer , other .analyzer )
190+ && Objects .equals (slop , other .slop )
191+ && Objects .equals (zeroTermsQuery , other .zeroTermsQuery );
159192 }
160193
161194 @ Override
@@ -169,6 +202,7 @@ public static MatchPhraseQueryBuilder fromXContent(XContentParser parser) throws
169202 float boost = AbstractQueryBuilder .DEFAULT_BOOST ;
170203 String analyzer = null ;
171204 int slop = MatchQuery .DEFAULT_PHRASE_SLOP ;
205+ ZeroTermsQuery zeroTermsQuery = MatchQuery .DEFAULT_ZERO_TERMS_QUERY ;
172206 String queryName = null ;
173207 String currentFieldName = null ;
174208 XContentParser .Token token ;
@@ -192,6 +226,16 @@ public static MatchPhraseQueryBuilder fromXContent(XContentParser parser) throws
192226 slop = parser .intValue ();
193227 } else if (AbstractQueryBuilder .NAME_FIELD .match (currentFieldName , parser .getDeprecationHandler ())) {
194228 queryName = parser .text ();
229+ } else if (ZERO_TERMS_QUERY_FIELD .match (currentFieldName , parser .getDeprecationHandler ())) {
230+ String zeroTermsDocs = parser .text ();
231+ if ("none" .equalsIgnoreCase (zeroTermsDocs )) {
232+ zeroTermsQuery = ZeroTermsQuery .NONE ;
233+ } else if ("all" .equalsIgnoreCase (zeroTermsDocs )) {
234+ zeroTermsQuery = ZeroTermsQuery .ALL ;
235+ } else {
236+ throw new ParsingException (parser .getTokenLocation (),
237+ "Unsupported zero_terms_docs value [" + zeroTermsDocs + "]" );
238+ }
195239 } else {
196240 throw new ParsingException (parser .getTokenLocation (),
197241 "[" + NAME + "] query does not support [" + currentFieldName + "]" );
@@ -211,6 +255,7 @@ public static MatchPhraseQueryBuilder fromXContent(XContentParser parser) throws
211255 MatchPhraseQueryBuilder matchQuery = new MatchPhraseQueryBuilder (fieldName , value );
212256 matchQuery .analyzer (analyzer );
213257 matchQuery .slop (slop );
258+ matchQuery .zeroTermsQuery (zeroTermsQuery );
214259 matchQuery .queryName (queryName );
215260 matchQuery .boost (boost );
216261 return matchQuery ;
0 commit comments