Skip to content

Commit 9110c28

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 30bd026 commit 9110c28

File tree

7 files changed

+48
-221
lines changed

7 files changed

+48
-221
lines changed

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

+14-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,13 @@ public MatchQueryBuilder(StreamInput in) throws IOException {
123110
super(in);
124111
fieldName = in.readString();
125112
value = in.readGenericValue();
126-
type = MatchQuery.Type.readFromStream(in);
113+
if (in.getVersion().before(Version.V_6_0_0_rc1)) {
114+
MatchQuery.Type.readFromStream(in); // deprecated type
115+
}
127116
operator = Operator.readFromStream(in);
128-
slop = in.readVInt();
117+
if (in.getVersion().before(Version.V_6_0_0_rc1)) {
118+
in.readVInt(); // deprecated slop
119+
}
129120
prefixLength = in.readVInt();
130121
maxExpansions = in.readVInt();
131122
fuzzyTranspositions = in.readBoolean();
@@ -146,9 +137,13 @@ public MatchQueryBuilder(StreamInput in) throws IOException {
146137
protected void doWriteTo(StreamOutput out) throws IOException {
147138
out.writeString(fieldName);
148139
out.writeGenericValue(value);
149-
type.writeTo(out);
140+
if (out.getVersion().before(Version.V_6_0_0_rc1)) {
141+
MatchQuery.Type.BOOLEAN.writeTo(out); // deprecated type
142+
}
150143
operator.writeTo(out);
151-
out.writeVInt(slop);
144+
if (out.getVersion().before(Version.V_6_0_0_rc1)) {
145+
out.writeVInt(MatchQuery.DEFAULT_PHRASE_SLOP); // deprecated slop
146+
}
152147
out.writeVInt(prefixLength);
153148
out.writeVInt(maxExpansions);
154149
out.writeBoolean(fuzzyTranspositions);
@@ -175,34 +170,6 @@ public Object value() {
175170
return this.value;
176171
}
177172

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-
206173
/** Sets the operator to use when using a boolean query. Defaults to <tt>OR</tt>. */
207174
public MatchQueryBuilder operator(Operator operator) {
208175
if (operator == null) {
@@ -231,30 +198,6 @@ public String analyzer() {
231198
return this.analyzer;
232199
}
233200

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-
258201
/** Sets the fuzziness used when evaluated to a fuzzy query type. Defaults to "AUTO". */
259202
public MatchQueryBuilder fuzziness(Object fuzziness) {
260203
this.fuzziness = Fuzziness.build(fuzziness);
@@ -425,18 +368,10 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio
425368
builder.startObject(fieldName);
426369

427370
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-
}
432371
builder.field(OPERATOR_FIELD.getPreferredName(), operator.toString());
433372
if (analyzer != null) {
434373
builder.field(ANALYZER_FIELD.getPreferredName(), analyzer);
435374
}
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-
}
440375
if (fuzziness != null) {
441376
fuzziness.toXContent(builder, params);
442377
}
@@ -473,7 +408,6 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
473408
if (analyzer != null) {
474409
matchQuery.setAnalyzer(analyzer);
475410
}
476-
matchQuery.setPhraseSlop(slop);
477411
matchQuery.setFuzziness(fuzziness);
478412
matchQuery.setFuzzyPrefixLength(prefixLength);
479413
matchQuery.setMaxExpansions(maxExpansions);
@@ -484,18 +418,16 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
484418
matchQuery.setZeroTermsQuery(zeroTermsQuery);
485419
matchQuery.setAutoGenerateSynonymsPhraseQuery(autoGenerateSynonymsPhraseQuery);
486420

487-
Query query = matchQuery.parse(type, fieldName, value);
421+
Query query = matchQuery.parse(MatchQuery.Type.BOOLEAN, fieldName, value);
488422
return Queries.maybeApplyMinimumShouldMatch(query, minimumShouldMatch);
489423
}
490424

491425
@Override
492426
protected boolean doEquals(MatchQueryBuilder other) {
493427
return Objects.equals(fieldName, other.fieldName) &&
494428
Objects.equals(value, other.value) &&
495-
Objects.equals(type, other.type) &&
496429
Objects.equals(operator, other.operator) &&
497430
Objects.equals(analyzer, other.analyzer) &&
498-
Objects.equals(slop, other.slop) &&
499431
Objects.equals(fuzziness, other.fuzziness) &&
500432
Objects.equals(prefixLength, other.prefixLength) &&
501433
Objects.equals(maxExpansions, other.maxExpansions) &&
@@ -510,7 +442,7 @@ protected boolean doEquals(MatchQueryBuilder other) {
510442

511443
@Override
512444
protected int doHashCode() {
513-
return Objects.hash(fieldName, value, type, operator, analyzer, slop,
445+
return Objects.hash(fieldName, value, operator, analyzer,
514446
fuzziness, prefixLength, maxExpansions, minimumShouldMatch,
515447
fuzzyRewrite, lenient, fuzzyTranspositions, zeroTermsQuery, cutoffFrequency, autoGenerateSynonymsPhraseQuery);
516448
}
@@ -522,13 +454,11 @@ public String getWriteableName() {
522454

523455
public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOException {
524456
String fieldName = null;
525-
MatchQuery.Type type = MatchQuery.Type.BOOLEAN;
526457
Object value = null;
527458
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
528459
String minimumShouldMatch = null;
529460
String analyzer = null;
530461
Operator operator = MatchQueryBuilder.DEFAULT_OPERATOR;
531-
int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
532462
Fuzziness fuzziness = null;
533463
int prefixLength = FuzzyQuery.defaultPrefixLength;
534464
int maxExpansion = FuzzyQuery.defaultMaxExpansions;
@@ -553,23 +483,10 @@ public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOExc
553483
} else if (token.isValue()) {
554484
if (QUERY_FIELD.match(currentFieldName)) {
555485
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-
}
567486
} else if (ANALYZER_FIELD.match(currentFieldName)) {
568487
analyzer = parser.text();
569488
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
570489
boost = parser.floatValue();
571-
} else if (SLOP_FIELD.match(currentFieldName)) {
572-
slop = parser.intValue();
573490
} else if (Fuzziness.FIELD.match(currentFieldName)) {
574491
fuzziness = Fuzziness.parse(parser);
575492
} else if (PREFIX_LENGTH_FIELD.match(currentFieldName)) {
@@ -624,9 +541,7 @@ public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOExc
624541

625542
MatchQueryBuilder matchQuery = new MatchQueryBuilder(fieldName, value);
626543
matchQuery.operator(operator);
627-
matchQuery.type(type);
628544
matchQuery.analyzer(analyzer);
629-
matchQuery.slop(slop);
630545
matchQuery.minimumShouldMatch(minimumShouldMatch);
631546
if (fuzziness != null) {
632547
matchQuery.fuzziness(fuzziness);

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

+1-89
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;
@@ -40,7 +38,6 @@
4038
import org.elasticsearch.index.mapper.MappedFieldType;
4139
import org.elasticsearch.index.mapper.MapperService;
4240
import org.elasticsearch.index.search.MatchQuery;
43-
import org.elasticsearch.index.search.MatchQuery.Type;
4441
import org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery;
4542
import org.elasticsearch.search.internal.SearchContext;
4643
import org.elasticsearch.test.AbstractQueryTestCase;
@@ -148,23 +145,6 @@ protected void doAssertLuceneQuery(MatchQueryBuilder queryBuilder, Query query,
148145
return;
149146
}
150147

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-
}
168148
QueryShardContext context = searchContext.getQueryShardContext();
169149
MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName());
170150
if (query instanceof TermQuery && fieldType != null) {
@@ -250,11 +230,6 @@ public void testIllegalValues() {
250230
assertEquals("[match] requires operator to be non-null", e.getMessage());
251231
}
252232

253-
{
254-
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> matchQuery.type(null));
255-
assertEquals("[match] requires type to be non-null", e.getMessage());
256-
}
257-
258233
{
259234
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> matchQuery.zeroTermsQuery(null));
260235
assertEquals("[match] requires zeroTermsQuery to be non-null", e.getMessage());
@@ -290,69 +265,6 @@ public void testSimpleMatchQuery() throws IOException {
290265
assertEquals(json, Operator.AND, qb.operator());
291266
}
292267

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-
356268
public void testFuzzinessOnNonStringField() throws Exception {
357269
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
358270
MatchQueryBuilder query = new MatchQueryBuilder(INT_FIELD_NAME, 42);
@@ -474,7 +386,7 @@ public void testLenientPhraseQuery() throws Exception {
474386
QueryShardContext context = createShardContext();
475387
MatchQuery b = new MatchQuery(context);
476388
b.setLenient(true);
477-
Query query = b.parse(Type.PHRASE, "string_no_pos", "foo bar");
389+
Query query = b.parse(MatchQuery.Type.PHRASE, "string_no_pos", "foo bar");
478390
assertThat(query, instanceOf(MatchNoDocsQuery.class));
479391
assertThat(query.toString(),
480392
containsString("field:[string_no_pos] was indexed without position data; cannot run PhraseQuery"));

0 commit comments

Comments
 (0)