Skip to content

Commit 22e200e

Browse files
authored
Remove deprecated type and slop field in MatchQueryBuilder (#26720)
The `type` field has been deprecated in 5.0 and can be removed. It has been replaced by using the MatchPhraseQueryBuilder or the MatchPhrasePrefixQueryBuilder. The `slop` field has also been deprecated and can be removed, the phrase and phrase prefix query builders still provide this parameter.
1 parent 5f40706 commit 22e200e

File tree

6 files changed

+47
-219
lines changed

6 files changed

+47
-219
lines changed

core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java

+18-99
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,13 @@
3535
import org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery;
3636

3737
import java.io.IOException;
38-
import java.util.Locale;
3938
import java.util.Objects;
4039

4140
/**
4241
* Match query is a query that analyzes the text and constructs a query as the
4342
* result of the analysis.
4443
*/
4544
public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
46-
public static final ParseField SLOP_FIELD = new ParseField("slop", "phrase_slop").withAllDeprecated("match_phrase query");
4745
public static final ParseField ZERO_TERMS_QUERY_FIELD = new ParseField("zero_terms_query");
4846
public static final ParseField CUTOFF_FREQUENCY_FIELD = new ParseField("cutoff_frequency");
4947
public static final ParseField LENIENT_FIELD = new ParseField("lenient");
@@ -54,7 +52,6 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
5452
public static final ParseField MAX_EXPANSIONS_FIELD = new ParseField("max_expansions");
5553
public static final ParseField PREFIX_LENGTH_FIELD = new ParseField("prefix_length");
5654
public static final ParseField ANALYZER_FIELD = new ParseField("analyzer");
57-
public static final ParseField TYPE_FIELD = new ParseField("type").withAllDeprecated("match_phrase and match_phrase_prefix query");
5855
public static final ParseField QUERY_FIELD = new ParseField("query");
5956
public static final ParseField GENERATE_SYNONYMS_PHRASE_QUERY = new ParseField("auto_generate_synonyms_phrase_query");
6057

@@ -64,24 +61,14 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
6461
/** The default mode terms are combined in a match query */
6562
public static final Operator DEFAULT_OPERATOR = Operator.OR;
6663

67-
/** The default mode match query type */
68-
@Deprecated
69-
public static final MatchQuery.Type DEFAULT_TYPE = MatchQuery.Type.BOOLEAN;
70-
7164
private final String fieldName;
7265

7366
private final Object value;
7467

75-
@Deprecated
76-
private MatchQuery.Type type = DEFAULT_TYPE;
77-
7868
private Operator operator = DEFAULT_OPERATOR;
7969

8070
private String analyzer;
8171

82-
@Deprecated
83-
private int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
84-
8572
private Fuzziness fuzziness = null;
8673

8774
private int prefixLength = FuzzyQuery.defaultPrefixLength;
@@ -123,9 +110,15 @@ public MatchQueryBuilder(StreamInput in) throws IOException {
123110
super(in);
124111
fieldName = in.readString();
125112
value = in.readGenericValue();
126-
type = MatchQuery.Type.readFromStream(in);
113+
// TODO lower this version once this has been backported to 6.0.0
114+
if (in.getVersion().before(Version.V_7_0_0_alpha1)) {
115+
MatchQuery.Type.readFromStream(in); // deprecated type
116+
}
127117
operator = Operator.readFromStream(in);
128-
slop = in.readVInt();
118+
// TODO lower this version once this has been backported to 6.0.0
119+
if (in.getVersion().before(Version.V_7_0_0_alpha1)) {
120+
in.readVInt(); // deprecated slop
121+
}
129122
prefixLength = in.readVInt();
130123
maxExpansions = in.readVInt();
131124
fuzzyTranspositions = in.readBoolean();
@@ -146,9 +139,15 @@ public MatchQueryBuilder(StreamInput in) throws IOException {
146139
protected void doWriteTo(StreamOutput out) throws IOException {
147140
out.writeString(fieldName);
148141
out.writeGenericValue(value);
149-
type.writeTo(out);
142+
// TODO lower this version once this has been backported to 6.0.0
143+
if (out.getVersion().before(Version.V_7_0_0_alpha1)) {
144+
MatchQuery.Type.BOOLEAN.writeTo(out); // deprecated type
145+
}
150146
operator.writeTo(out);
151-
out.writeVInt(slop);
147+
// TODO lower this version once this has been backported to 6.0.0
148+
if (out.getVersion().before(Version.V_7_0_0_alpha1)) {
149+
out.writeVInt(MatchQuery.DEFAULT_PHRASE_SLOP); // deprecated slop
150+
}
152151
out.writeVInt(prefixLength);
153152
out.writeVInt(maxExpansions);
154153
out.writeBoolean(fuzzyTranspositions);
@@ -175,34 +174,6 @@ public Object value() {
175174
return this.value;
176175
}
177176

178-
/**
179-
* Sets the type of the text query.
180-
*
181-
* @deprecated Use {@link MatchPhraseQueryBuilder} for <code>phrase</code>
182-
* queries and {@link MatchPhrasePrefixQueryBuilder} for
183-
* <code>phrase_prefix</code> queries
184-
*/
185-
@Deprecated
186-
public MatchQueryBuilder type(MatchQuery.Type type) {
187-
if (type == null) {
188-
throw new IllegalArgumentException("[" + NAME + "] requires type to be non-null");
189-
}
190-
this.type = type;
191-
return this;
192-
}
193-
194-
/**
195-
* Get the type of the query.
196-
*
197-
* @deprecated Use {@link MatchPhraseQueryBuilder} for <code>phrase</code>
198-
* queries and {@link MatchPhrasePrefixQueryBuilder} for
199-
* <code>phrase_prefix</code> queries
200-
*/
201-
@Deprecated
202-
public MatchQuery.Type type() {
203-
return this.type;
204-
}
205-
206177
/** Sets the operator to use when using a boolean query. Defaults to <tt>OR</tt>. */
207178
public MatchQueryBuilder operator(Operator operator) {
208179
if (operator == null) {
@@ -231,30 +202,6 @@ public String analyzer() {
231202
return this.analyzer;
232203
}
233204

234-
/**
235-
* Sets a slop factor for phrase queries
236-
*
237-
* @deprecated for phrase queries use {@link MatchPhraseQueryBuilder}
238-
*/
239-
@Deprecated
240-
public MatchQueryBuilder slop(int slop) {
241-
if (slop < 0 ) {
242-
throw new IllegalArgumentException("No negative slop allowed.");
243-
}
244-
this.slop = slop;
245-
return this;
246-
}
247-
248-
/**
249-
* Get the slop factor for phrase queries.
250-
*
251-
* @deprecated for phrase queries use {@link MatchPhraseQueryBuilder}
252-
*/
253-
@Deprecated
254-
public int slop() {
255-
return this.slop;
256-
}
257-
258205
/** Sets the fuzziness used when evaluated to a fuzzy query type. Defaults to "AUTO". */
259206
public MatchQueryBuilder fuzziness(Object fuzziness) {
260207
this.fuzziness = Fuzziness.build(fuzziness);
@@ -425,18 +372,10 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio
425372
builder.startObject(fieldName);
426373

427374
builder.field(QUERY_FIELD.getPreferredName(), value);
428-
// this is deprecated so only output the value if its not the default value (for bwc)
429-
if (type != MatchQuery.Type.BOOLEAN) {
430-
builder.field(TYPE_FIELD.getPreferredName(), type.toString().toLowerCase(Locale.ENGLISH));
431-
}
432375
builder.field(OPERATOR_FIELD.getPreferredName(), operator.toString());
433376
if (analyzer != null) {
434377
builder.field(ANALYZER_FIELD.getPreferredName(), analyzer);
435378
}
436-
// this is deprecated so only output the value if its not the default value (for bwc)
437-
if (slop != MatchQuery.DEFAULT_PHRASE_SLOP) {
438-
builder.field(SLOP_FIELD.getPreferredName(), slop);
439-
}
440379
if (fuzziness != null) {
441380
fuzziness.toXContent(builder, params);
442381
}
@@ -473,7 +412,6 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
473412
if (analyzer != null) {
474413
matchQuery.setAnalyzer(analyzer);
475414
}
476-
matchQuery.setPhraseSlop(slop);
477415
matchQuery.setFuzziness(fuzziness);
478416
matchQuery.setFuzzyPrefixLength(prefixLength);
479417
matchQuery.setMaxExpansions(maxExpansions);
@@ -484,18 +422,16 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
484422
matchQuery.setZeroTermsQuery(zeroTermsQuery);
485423
matchQuery.setAutoGenerateSynonymsPhraseQuery(autoGenerateSynonymsPhraseQuery);
486424

487-
Query query = matchQuery.parse(type, fieldName, value);
425+
Query query = matchQuery.parse(MatchQuery.Type.BOOLEAN, fieldName, value);
488426
return Queries.maybeApplyMinimumShouldMatch(query, minimumShouldMatch);
489427
}
490428

491429
@Override
492430
protected boolean doEquals(MatchQueryBuilder other) {
493431
return Objects.equals(fieldName, other.fieldName) &&
494432
Objects.equals(value, other.value) &&
495-
Objects.equals(type, other.type) &&
496433
Objects.equals(operator, other.operator) &&
497434
Objects.equals(analyzer, other.analyzer) &&
498-
Objects.equals(slop, other.slop) &&
499435
Objects.equals(fuzziness, other.fuzziness) &&
500436
Objects.equals(prefixLength, other.prefixLength) &&
501437
Objects.equals(maxExpansions, other.maxExpansions) &&
@@ -510,7 +446,7 @@ protected boolean doEquals(MatchQueryBuilder other) {
510446

511447
@Override
512448
protected int doHashCode() {
513-
return Objects.hash(fieldName, value, type, operator, analyzer, slop,
449+
return Objects.hash(fieldName, value, operator, analyzer,
514450
fuzziness, prefixLength, maxExpansions, minimumShouldMatch,
515451
fuzzyRewrite, lenient, fuzzyTranspositions, zeroTermsQuery, cutoffFrequency, autoGenerateSynonymsPhraseQuery);
516452
}
@@ -522,13 +458,11 @@ public String getWriteableName() {
522458

523459
public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOException {
524460
String fieldName = null;
525-
MatchQuery.Type type = MatchQuery.Type.BOOLEAN;
526461
Object value = null;
527462
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
528463
String minimumShouldMatch = null;
529464
String analyzer = null;
530465
Operator operator = MatchQueryBuilder.DEFAULT_OPERATOR;
531-
int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
532466
Fuzziness fuzziness = null;
533467
int prefixLength = FuzzyQuery.defaultPrefixLength;
534468
int maxExpansion = FuzzyQuery.defaultMaxExpansions;
@@ -553,23 +487,10 @@ public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOExc
553487
} else if (token.isValue()) {
554488
if (QUERY_FIELD.match(currentFieldName)) {
555489
value = parser.objectText();
556-
} else if (TYPE_FIELD.match(currentFieldName)) {
557-
String tStr = parser.text();
558-
if ("boolean".equals(tStr)) {
559-
type = MatchQuery.Type.BOOLEAN;
560-
} else if ("phrase".equals(tStr)) {
561-
type = MatchQuery.Type.PHRASE;
562-
} else if ("phrase_prefix".equals(tStr) || ("phrasePrefix".equals(tStr))) {
563-
type = MatchQuery.Type.PHRASE_PREFIX;
564-
} else {
565-
throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] query does not support type " + tStr);
566-
}
567490
} else if (ANALYZER_FIELD.match(currentFieldName)) {
568491
analyzer = parser.text();
569492
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
570493
boost = parser.floatValue();
571-
} else if (SLOP_FIELD.match(currentFieldName)) {
572-
slop = parser.intValue();
573494
} else if (Fuzziness.FIELD.match(currentFieldName)) {
574495
fuzziness = Fuzziness.parse(parser);
575496
} else if (PREFIX_LENGTH_FIELD.match(currentFieldName)) {
@@ -624,9 +545,7 @@ public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOExc
624545

625546
MatchQueryBuilder matchQuery = new MatchQueryBuilder(fieldName, value);
626547
matchQuery.operator(operator);
627-
matchQuery.type(type);
628548
matchQuery.analyzer(analyzer);
629-
matchQuery.slop(slop);
630549
matchQuery.minimumShouldMatch(minimumShouldMatch);
631550
if (fuzziness != null) {
632551
matchQuery.fuzziness(fuzziness);

core/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java

-87
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424
import org.apache.lucene.search.BooleanQuery;
2525
import org.apache.lucene.search.BoostQuery;
2626
import org.apache.lucene.search.FuzzyQuery;
27-
import org.apache.lucene.search.IndexOrDocValuesQuery;
2827
import org.apache.lucene.search.MatchAllDocsQuery;
2928
import org.apache.lucene.search.MatchNoDocsQuery;
30-
import org.apache.lucene.search.PhraseQuery;
3129
import org.apache.lucene.search.PointRangeQuery;
3230
import org.apache.lucene.search.Query;
3331
import org.apache.lucene.search.TermQuery;
@@ -148,23 +146,6 @@ protected void doAssertLuceneQuery(MatchQueryBuilder queryBuilder, Query query,
148146
return;
149147
}
150148

151-
switch (queryBuilder.type()) {
152-
case BOOLEAN:
153-
assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(ExtendedCommonTermsQuery.class))
154-
.or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class)).or(instanceOf(MatchNoDocsQuery.class))
155-
.or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
156-
break;
157-
case PHRASE:
158-
assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(PhraseQuery.class))
159-
.or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class))
160-
.or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
161-
break;
162-
case PHRASE_PREFIX:
163-
assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(MultiPhrasePrefixQuery.class))
164-
.or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class))
165-
.or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
166-
break;
167-
}
168149
QueryShardContext context = searchContext.getQueryShardContext();
169150
MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName());
170151
if (query instanceof TermQuery && fieldType != null) {
@@ -250,11 +231,6 @@ public void testIllegalValues() {
250231
assertEquals("[match] requires operator to be non-null", e.getMessage());
251232
}
252233

253-
{
254-
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> matchQuery.type(null));
255-
assertEquals("[match] requires type to be non-null", e.getMessage());
256-
}
257-
258234
{
259235
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> matchQuery.zeroTermsQuery(null));
260236
assertEquals("[match] requires zeroTermsQuery to be non-null", e.getMessage());
@@ -290,69 +266,6 @@ public void testSimpleMatchQuery() throws IOException {
290266
assertEquals(json, Operator.AND, qb.operator());
291267
}
292268

293-
public void testLegacyMatchPhrasePrefixQuery() throws IOException {
294-
MatchQueryBuilder expectedQB = new MatchQueryBuilder("message", "to be or not to be");
295-
expectedQB.type(Type.PHRASE_PREFIX);
296-
expectedQB.slop(2);
297-
expectedQB.maxExpansions(30);
298-
String json = "{\n" +
299-
" \"match\" : {\n" +
300-
" \"message\" : {\n" +
301-
" \"query\" : \"to be or not to be\",\n" +
302-
" \"type\" : \"phrase_prefix\",\n" +
303-
" \"operator\" : \"OR\",\n" +
304-
" \"slop\" : 2,\n" +
305-
" \"prefix_length\" : 0,\n" +
306-
" \"max_expansions\" : 30,\n" +
307-
" \"fuzzy_transpositions\" : true,\n" +
308-
" \"lenient\" : false,\n" +
309-
" \"zero_terms_query\" : \"NONE\",\n" +
310-
" \"auto_generate_synonyms_phrase_query\" : true,\n" +
311-
" \"boost\" : 1.0\n" +
312-
" }\n" +
313-
" }\n" +
314-
"}";
315-
MatchQueryBuilder qb = (MatchQueryBuilder) parseQuery(json);
316-
checkGeneratedJson(json, qb);
317-
318-
assertEquals(json, expectedQB, qb);
319-
320-
assertSerialization(qb);
321-
322-
assertWarnings("Deprecated field [type] used, replaced by [match_phrase and match_phrase_prefix query]",
323-
"Deprecated field [slop] used, replaced by [match_phrase query]");
324-
}
325-
326-
public void testLegacyMatchPhraseQuery() throws IOException {
327-
MatchQueryBuilder expectedQB = new MatchQueryBuilder("message", "to be or not to be");
328-
expectedQB.type(Type.PHRASE);
329-
expectedQB.slop(2);
330-
String json = "{\n" +
331-
" \"match\" : {\n" +
332-
" \"message\" : {\n" +
333-
" \"query\" : \"to be or not to be\",\n" +
334-
" \"type\" : \"phrase\",\n" +
335-
" \"operator\" : \"OR\",\n" +
336-
" \"slop\" : 2,\n" +
337-
" \"prefix_length\" : 0,\n" +
338-
" \"max_expansions\" : 50,\n" +
339-
" \"fuzzy_transpositions\" : true,\n" +
340-
" \"lenient\" : false,\n" +
341-
" \"zero_terms_query\" : \"NONE\",\n" +
342-
" \"auto_generate_synonyms_phrase_query\" : true,\n" +
343-
" \"boost\" : 1.0\n" +
344-
" }\n" +
345-
" }\n" +
346-
"}";
347-
MatchQueryBuilder qb = (MatchQueryBuilder) parseQuery(json);
348-
checkGeneratedJson(json, qb);
349-
350-
assertEquals(json, expectedQB, qb);
351-
assertSerialization(qb);
352-
assertWarnings("Deprecated field [type] used, replaced by [match_phrase and match_phrase_prefix query]",
353-
"Deprecated field [slop] used, replaced by [match_phrase query]");
354-
}
355-
356269
public void testFuzzinessOnNonStringField() throws Exception {
357270
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
358271
MatchQueryBuilder query = new MatchQueryBuilder(INT_FIELD_NAME, 42);

0 commit comments

Comments
 (0)