Skip to content

Commit b63b023

Browse files
committed
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 943cbd7 commit b63b023

File tree

7 files changed

+48
-218
lines changed

7 files changed

+48
-218
lines changed

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

+15-99
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.lucene.search.FuzzyQuery;
2323
import org.apache.lucene.search.Query;
24+
import org.elasticsearch.Version;
2425
import org.elasticsearch.common.ParseField;
2526
import org.elasticsearch.common.ParsingException;
2627
import org.elasticsearch.common.io.stream.StreamInput;
@@ -34,15 +35,13 @@
3435
import org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery;
3536

3637
import java.io.IOException;
37-
import java.util.Locale;
3838
import java.util.Objects;
3939

4040
/**
4141
* Match query is a query that analyzes the text and constructs a query as the
4242
* result of the analysis.
4343
*/
4444
public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
45-
public static final ParseField SLOP_FIELD = new ParseField("slop", "phrase_slop").withAllDeprecated("match_phrase query");
4645
public static final ParseField ZERO_TERMS_QUERY_FIELD = new ParseField("zero_terms_query");
4746
public static final ParseField CUTOFF_FREQUENCY_FIELD = new ParseField("cutoff_frequency");
4847
public static final ParseField LENIENT_FIELD = new ParseField("lenient");
@@ -53,7 +52,6 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
5352
public static final ParseField MAX_EXPANSIONS_FIELD = new ParseField("max_expansions");
5453
public static final ParseField PREFIX_LENGTH_FIELD = new ParseField("prefix_length");
5554
public static final ParseField ANALYZER_FIELD = new ParseField("analyzer");
56-
public static final ParseField TYPE_FIELD = new ParseField("type").withAllDeprecated("match_phrase and match_phrase_prefix query");
5755
public static final ParseField QUERY_FIELD = new ParseField("query");
5856

5957
/** The name for the match query */
@@ -62,24 +60,14 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
6260
/** The default mode terms are combined in a match query */
6361
public static final Operator DEFAULT_OPERATOR = Operator.OR;
6462

65-
/** The default mode match query type */
66-
@Deprecated
67-
public static final MatchQuery.Type DEFAULT_TYPE = MatchQuery.Type.BOOLEAN;
68-
6963
private final String fieldName;
7064

7165
private final Object value;
7266

73-
@Deprecated
74-
private MatchQuery.Type type = DEFAULT_TYPE;
75-
7667
private Operator operator = DEFAULT_OPERATOR;
7768

7869
private String analyzer;
7970

80-
@Deprecated
81-
private int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
82-
8371
private Fuzziness fuzziness = null;
8472

8573
private int prefixLength = FuzzyQuery.defaultPrefixLength;
@@ -119,9 +107,13 @@ public MatchQueryBuilder(StreamInput in) throws IOException {
119107
super(in);
120108
fieldName = in.readString();
121109
value = in.readGenericValue();
122-
type = MatchQuery.Type.readFromStream(in);
110+
if (in.getVersion().before(Version.V_6_0_0_rc1)) {
111+
MatchQuery.Type.readFromStream(in); // deprecated type
112+
}
123113
operator = Operator.readFromStream(in);
124-
slop = in.readVInt();
114+
if (in.getVersion().before(Version.V_6_0_0_rc1)) {
115+
in.readVInt(); // deprecated slop
116+
}
125117
prefixLength = in.readVInt();
126118
maxExpansions = in.readVInt();
127119
fuzzyTranspositions = in.readBoolean();
@@ -139,9 +131,13 @@ public MatchQueryBuilder(StreamInput in) throws IOException {
139131
protected void doWriteTo(StreamOutput out) throws IOException {
140132
out.writeString(fieldName);
141133
out.writeGenericValue(value);
142-
type.writeTo(out);
134+
if (out.getVersion().before(Version.V_6_0_0_rc1)) {
135+
MatchQuery.Type.BOOLEAN.writeTo(out); // deprecated type
136+
}
143137
operator.writeTo(out);
144-
out.writeVInt(slop);
138+
if (out.getVersion().before(Version.V_6_0_0_rc1)) {
139+
out.writeVInt(MatchQuery.DEFAULT_PHRASE_SLOP); // deprecated slop
140+
}
145141
out.writeVInt(prefixLength);
146142
out.writeVInt(maxExpansions);
147143
out.writeBoolean(fuzzyTranspositions);
@@ -165,34 +161,6 @@ public Object value() {
165161
return this.value;
166162
}
167163

168-
/**
169-
* Sets the type of the text query.
170-
*
171-
* @deprecated Use {@link MatchPhraseQueryBuilder} for <code>phrase</code>
172-
* queries and {@link MatchPhrasePrefixQueryBuilder} for
173-
* <code>phrase_prefix</code> queries
174-
*/
175-
@Deprecated
176-
public MatchQueryBuilder type(MatchQuery.Type type) {
177-
if (type == null) {
178-
throw new IllegalArgumentException("[" + NAME + "] requires type to be non-null");
179-
}
180-
this.type = type;
181-
return this;
182-
}
183-
184-
/**
185-
* Get the type of the query.
186-
*
187-
* @deprecated Use {@link MatchPhraseQueryBuilder} for <code>phrase</code>
188-
* queries and {@link MatchPhrasePrefixQueryBuilder} for
189-
* <code>phrase_prefix</code> queries
190-
*/
191-
@Deprecated
192-
public MatchQuery.Type type() {
193-
return this.type;
194-
}
195-
196164
/** Sets the operator to use when using a boolean query. Defaults to <tt>OR</tt>. */
197165
public MatchQueryBuilder operator(Operator operator) {
198166
if (operator == null) {
@@ -221,30 +189,6 @@ public String analyzer() {
221189
return this.analyzer;
222190
}
223191

224-
/**
225-
* Sets a slop factor for phrase queries
226-
*
227-
* @deprecated for phrase queries use {@link MatchPhraseQueryBuilder}
228-
*/
229-
@Deprecated
230-
public MatchQueryBuilder slop(int slop) {
231-
if (slop < 0 ) {
232-
throw new IllegalArgumentException("No negative slop allowed.");
233-
}
234-
this.slop = slop;
235-
return this;
236-
}
237-
238-
/**
239-
* Get the slop factor for phrase queries.
240-
*
241-
* @deprecated for phrase queries use {@link MatchPhraseQueryBuilder}
242-
*/
243-
@Deprecated
244-
public int slop() {
245-
return this.slop;
246-
}
247-
248192
/** Sets the fuzziness used when evaluated to a fuzzy query type. Defaults to "AUTO". */
249193
public MatchQueryBuilder fuzziness(Object fuzziness) {
250194
this.fuzziness = Fuzziness.build(fuzziness);
@@ -401,18 +345,10 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio
401345
builder.startObject(fieldName);
402346

403347
builder.field(QUERY_FIELD.getPreferredName(), value);
404-
// this is deprecated so only output the value if its not the default value (for bwc)
405-
if (type != MatchQuery.Type.BOOLEAN) {
406-
builder.field(TYPE_FIELD.getPreferredName(), type.toString().toLowerCase(Locale.ENGLISH));
407-
}
408348
builder.field(OPERATOR_FIELD.getPreferredName(), operator.toString());
409349
if (analyzer != null) {
410350
builder.field(ANALYZER_FIELD.getPreferredName(), analyzer);
411351
}
412-
// this is deprecated so only output the value if its not the default value (for bwc)
413-
if (slop != MatchQuery.DEFAULT_PHRASE_SLOP) {
414-
builder.field(SLOP_FIELD.getPreferredName(), slop);
415-
}
416352
if (fuzziness != null) {
417353
fuzziness.toXContent(builder, params);
418354
}
@@ -448,7 +384,6 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
448384
if (analyzer != null) {
449385
matchQuery.setAnalyzer(analyzer);
450386
}
451-
matchQuery.setPhraseSlop(slop);
452387
matchQuery.setFuzziness(fuzziness);
453388
matchQuery.setFuzzyPrefixLength(prefixLength);
454389
matchQuery.setMaxExpansions(maxExpansions);
@@ -458,18 +393,16 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
458393
matchQuery.setCommonTermsCutoff(cutoffFrequency);
459394
matchQuery.setZeroTermsQuery(zeroTermsQuery);
460395

461-
Query query = matchQuery.parse(type, fieldName, value);
396+
Query query = matchQuery.parse(MatchQuery.Type.BOOLEAN, fieldName, value);
462397
return Queries.maybeApplyMinimumShouldMatch(query, minimumShouldMatch);
463398
}
464399

465400
@Override
466401
protected boolean doEquals(MatchQueryBuilder other) {
467402
return Objects.equals(fieldName, other.fieldName) &&
468403
Objects.equals(value, other.value) &&
469-
Objects.equals(type, other.type) &&
470404
Objects.equals(operator, other.operator) &&
471405
Objects.equals(analyzer, other.analyzer) &&
472-
Objects.equals(slop, other.slop) &&
473406
Objects.equals(fuzziness, other.fuzziness) &&
474407
Objects.equals(prefixLength, other.prefixLength) &&
475408
Objects.equals(maxExpansions, other.maxExpansions) &&
@@ -483,7 +416,7 @@ protected boolean doEquals(MatchQueryBuilder other) {
483416

484417
@Override
485418
protected int doHashCode() {
486-
return Objects.hash(fieldName, value, type, operator, analyzer, slop,
419+
return Objects.hash(fieldName, value, operator, analyzer,
487420
fuzziness, prefixLength, maxExpansions, minimumShouldMatch,
488421
fuzzyRewrite, lenient, fuzzyTranspositions, zeroTermsQuery, cutoffFrequency);
489422
}
@@ -495,13 +428,11 @@ public String getWriteableName() {
495428

496429
public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOException {
497430
String fieldName = null;
498-
MatchQuery.Type type = MatchQuery.Type.BOOLEAN;
499431
Object value = null;
500432
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
501433
String minimumShouldMatch = null;
502434
String analyzer = null;
503435
Operator operator = MatchQueryBuilder.DEFAULT_OPERATOR;
504-
int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
505436
Fuzziness fuzziness = null;
506437
int prefixLength = FuzzyQuery.defaultPrefixLength;
507438
int maxExpansion = FuzzyQuery.defaultMaxExpansions;
@@ -525,23 +456,10 @@ public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOExc
525456
} else if (token.isValue()) {
526457
if (QUERY_FIELD.match(currentFieldName)) {
527458
value = parser.objectText();
528-
} else if (TYPE_FIELD.match(currentFieldName)) {
529-
String tStr = parser.text();
530-
if ("boolean".equals(tStr)) {
531-
type = MatchQuery.Type.BOOLEAN;
532-
} else if ("phrase".equals(tStr)) {
533-
type = MatchQuery.Type.PHRASE;
534-
} else if ("phrase_prefix".equals(tStr) || ("phrasePrefix".equals(tStr))) {
535-
type = MatchQuery.Type.PHRASE_PREFIX;
536-
} else {
537-
throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] query does not support type " + tStr);
538-
}
539459
} else if (ANALYZER_FIELD.match(currentFieldName)) {
540460
analyzer = parser.text();
541461
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
542462
boost = parser.floatValue();
543-
} else if (SLOP_FIELD.match(currentFieldName)) {
544-
slop = parser.intValue();
545463
} else if (Fuzziness.FIELD.match(currentFieldName)) {
546464
fuzziness = Fuzziness.parse(parser);
547465
} else if (PREFIX_LENGTH_FIELD.match(currentFieldName)) {
@@ -594,9 +512,7 @@ public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOExc
594512

595513
MatchQueryBuilder matchQuery = new MatchQueryBuilder(fieldName, value);
596514
matchQuery.operator(operator);
597-
matchQuery.type(type);
598515
matchQuery.analyzer(analyzer);
599-
matchQuery.slop(slop);
600516
matchQuery.minimumShouldMatch(minimumShouldMatch);
601517
if (fuzziness != null) {
602518
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,7 @@
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;
29-
import org.apache.lucene.search.MatchNoDocsQuery;
30-
import org.apache.lucene.search.PhraseQuery;
3128
import org.apache.lucene.search.PointRangeQuery;
3229
import org.apache.lucene.search.Query;
3330
import org.apache.lucene.search.TermQuery;
@@ -40,7 +37,6 @@
4037
import org.elasticsearch.index.mapper.MappedFieldType;
4138
import org.elasticsearch.index.mapper.MapperService;
4239
import org.elasticsearch.index.search.MatchQuery;
43-
import org.elasticsearch.index.search.MatchQuery.Type;
4440
import org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery;
4541
import org.elasticsearch.search.internal.SearchContext;
4642
import org.elasticsearch.test.AbstractQueryTestCase;
@@ -144,23 +140,6 @@ protected void doAssertLuceneQuery(MatchQueryBuilder queryBuilder, Query query,
144140
return;
145141
}
146142

147-
switch (queryBuilder.type()) {
148-
case BOOLEAN:
149-
assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(ExtendedCommonTermsQuery.class))
150-
.or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class)).or(instanceOf(MatchNoDocsQuery.class))
151-
.or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
152-
break;
153-
case PHRASE:
154-
assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(PhraseQuery.class))
155-
.or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class))
156-
.or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
157-
break;
158-
case PHRASE_PREFIX:
159-
assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(MultiPhrasePrefixQuery.class))
160-
.or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class))
161-
.or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
162-
break;
163-
}
164143
QueryShardContext context = searchContext.getQueryShardContext();
165144
MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName());
166145
if (query instanceof TermQuery && fieldType != null) {
@@ -246,11 +225,6 @@ public void testIllegalValues() {
246225
assertEquals("[match] requires operator to be non-null", e.getMessage());
247226
}
248227

249-
{
250-
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> matchQuery.type(null));
251-
assertEquals("[match] requires type to be non-null", e.getMessage());
252-
}
253-
254228
{
255229
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> matchQuery.zeroTermsQuery(null));
256230
assertEquals("[match] requires zeroTermsQuery to be non-null", e.getMessage());
@@ -285,67 +259,6 @@ public void testSimpleMatchQuery() throws IOException {
285259
assertEquals(json, Operator.AND, qb.operator());
286260
}
287261

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

0 commit comments

Comments
 (0)