20
20
package org .elasticsearch .index .query ;
21
21
22
22
import org .apache .lucene .search .Query ;
23
+ import org .elasticsearch .Version ;
23
24
import org .elasticsearch .common .ParseField ;
24
25
import org .elasticsearch .common .ParsingException ;
25
26
import org .elasticsearch .common .Strings ;
28
29
import org .elasticsearch .common .xcontent .XContentBuilder ;
29
30
import org .elasticsearch .common .xcontent .XContentParser ;
30
31
import org .elasticsearch .index .search .MatchQuery ;
32
+ import org .elasticsearch .index .search .MatchQuery .ZeroTermsQuery ;
31
33
32
34
import java .io .IOException ;
33
35
import java .util .Objects ;
39
41
public class MatchPhraseQueryBuilder extends AbstractQueryBuilder <MatchPhraseQueryBuilder > {
40
42
public static final String NAME = "match_phrase" ;
41
43
public static final ParseField SLOP_FIELD = new ParseField ("slop" );
44
+ public static final ParseField ZERO_TERMS_QUERY_FIELD = new ParseField ("zero_terms_query" );
42
45
43
46
private final String fieldName ;
44
47
@@ -48,6 +51,8 @@ public class MatchPhraseQueryBuilder extends AbstractQueryBuilder<MatchPhraseQue
48
51
49
52
private int slop = MatchQuery .DEFAULT_PHRASE_SLOP ;
50
53
54
+ private ZeroTermsQuery zeroTermsQuery = MatchQuery .DEFAULT_ZERO_TERMS_QUERY ;
55
+
51
56
public MatchPhraseQueryBuilder (String fieldName , Object value ) {
52
57
if (Strings .isEmpty (fieldName )) {
53
58
throw new IllegalArgumentException ("[" + NAME + "] requires fieldName" );
@@ -67,6 +72,9 @@ public MatchPhraseQueryBuilder(StreamInput in) throws IOException {
67
72
fieldName = in .readString ();
68
73
value = in .readGenericValue ();
69
74
slop = in .readVInt ();
75
+ if (in .getVersion ().onOrAfter (Version .V_7_0_0_alpha1 )) {
76
+ zeroTermsQuery = ZeroTermsQuery .readFromStream (in );
77
+ }
70
78
analyzer = in .readOptionalString ();
71
79
}
72
80
@@ -75,6 +83,9 @@ protected void doWriteTo(StreamOutput out) throws IOException {
75
83
out .writeString (fieldName );
76
84
out .writeGenericValue (value );
77
85
out .writeVInt (slop );
86
+ if (out .getVersion ().onOrAfter (Version .V_7_0_0_alpha1 )) {
87
+ zeroTermsQuery .writeTo (out );
88
+ }
78
89
out .writeOptionalString (analyzer );
79
90
}
80
91
@@ -116,6 +127,23 @@ public int slop() {
116
127
return this .slop ;
117
128
}
118
129
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
+
119
147
@ Override
120
148
public String getWriteableName () {
121
149
return NAME ;
@@ -131,6 +159,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
131
159
builder .field (MatchQueryBuilder .ANALYZER_FIELD .getPreferredName (), analyzer );
132
160
}
133
161
builder .field (SLOP_FIELD .getPreferredName (), slop );
162
+ builder .field (ZERO_TERMS_QUERY_FIELD .getPreferredName (), zeroTermsQuery .toString ());
134
163
printBoostAndQueryName (builder );
135
164
builder .endObject ();
136
165
builder .endObject ();
@@ -148,14 +177,18 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
148
177
matchQuery .setAnalyzer (analyzer );
149
178
}
150
179
matchQuery .setPhraseSlop (slop );
180
+ matchQuery .setZeroTermsQuery (zeroTermsQuery );
151
181
152
182
return matchQuery .parse (MatchQuery .Type .PHRASE , fieldName , value );
153
183
}
154
184
155
185
@ Override
156
186
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 );
159
192
}
160
193
161
194
@ Override
@@ -169,6 +202,7 @@ public static MatchPhraseQueryBuilder fromXContent(XContentParser parser) throws
169
202
float boost = AbstractQueryBuilder .DEFAULT_BOOST ;
170
203
String analyzer = null ;
171
204
int slop = MatchQuery .DEFAULT_PHRASE_SLOP ;
205
+ ZeroTermsQuery zeroTermsQuery = MatchQuery .DEFAULT_ZERO_TERMS_QUERY ;
172
206
String queryName = null ;
173
207
String currentFieldName = null ;
174
208
XContentParser .Token token ;
@@ -192,6 +226,16 @@ public static MatchPhraseQueryBuilder fromXContent(XContentParser parser) throws
192
226
slop = parser .intValue ();
193
227
} else if (AbstractQueryBuilder .NAME_FIELD .match (currentFieldName , parser .getDeprecationHandler ())) {
194
228
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
+ }
195
239
} else {
196
240
throw new ParsingException (parser .getTokenLocation (),
197
241
"[" + NAME + "] query does not support [" + currentFieldName + "]" );
@@ -211,6 +255,7 @@ public static MatchPhraseQueryBuilder fromXContent(XContentParser parser) throws
211
255
MatchPhraseQueryBuilder matchQuery = new MatchPhraseQueryBuilder (fieldName , value );
212
256
matchQuery .analyzer (analyzer );
213
257
matchQuery .slop (slop );
258
+ matchQuery .zeroTermsQuery (zeroTermsQuery );
214
259
matchQuery .queryName (queryName );
215
260
matchQuery .boost (boost );
216
261
return matchQuery ;
0 commit comments