Skip to content

Commit

Permalink
Remove deprecated type and slop field in MatchQueryBuilder (#26720)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cbuescher committed Sep 20, 2017
1 parent 943cbd7 commit b63b023
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.Version;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
Expand All @@ -34,15 +35,13 @@
import org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery;

import java.io.IOException;
import java.util.Locale;
import java.util.Objects;

/**
* Match query is a query that analyzes the text and constructs a query as the
* result of the analysis.
*/
public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
public static final ParseField SLOP_FIELD = new ParseField("slop", "phrase_slop").withAllDeprecated("match_phrase query");
public static final ParseField ZERO_TERMS_QUERY_FIELD = new ParseField("zero_terms_query");
public static final ParseField CUTOFF_FREQUENCY_FIELD = new ParseField("cutoff_frequency");
public static final ParseField LENIENT_FIELD = new ParseField("lenient");
Expand All @@ -53,7 +52,6 @@ public class MatchQueryBuilder extends AbstractQueryBuilder<MatchQueryBuilder> {
public static final ParseField MAX_EXPANSIONS_FIELD = new ParseField("max_expansions");
public static final ParseField PREFIX_LENGTH_FIELD = new ParseField("prefix_length");
public static final ParseField ANALYZER_FIELD = new ParseField("analyzer");
public static final ParseField TYPE_FIELD = new ParseField("type").withAllDeprecated("match_phrase and match_phrase_prefix query");
public static final ParseField QUERY_FIELD = new ParseField("query");

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

/** The default mode match query type */
@Deprecated
public static final MatchQuery.Type DEFAULT_TYPE = MatchQuery.Type.BOOLEAN;

private final String fieldName;

private final Object value;

@Deprecated
private MatchQuery.Type type = DEFAULT_TYPE;

private Operator operator = DEFAULT_OPERATOR;

private String analyzer;

@Deprecated
private int slop = MatchQuery.DEFAULT_PHRASE_SLOP;

private Fuzziness fuzziness = null;

private int prefixLength = FuzzyQuery.defaultPrefixLength;
Expand Down Expand Up @@ -119,9 +107,13 @@ public MatchQueryBuilder(StreamInput in) throws IOException {
super(in);
fieldName = in.readString();
value = in.readGenericValue();
type = MatchQuery.Type.readFromStream(in);
if (in.getVersion().before(Version.V_6_0_0_rc1)) {
MatchQuery.Type.readFromStream(in); // deprecated type
}
operator = Operator.readFromStream(in);
slop = in.readVInt();
if (in.getVersion().before(Version.V_6_0_0_rc1)) {
in.readVInt(); // deprecated slop
}
prefixLength = in.readVInt();
maxExpansions = in.readVInt();
fuzzyTranspositions = in.readBoolean();
Expand All @@ -139,9 +131,13 @@ public MatchQueryBuilder(StreamInput in) throws IOException {
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeString(fieldName);
out.writeGenericValue(value);
type.writeTo(out);
if (out.getVersion().before(Version.V_6_0_0_rc1)) {
MatchQuery.Type.BOOLEAN.writeTo(out); // deprecated type
}
operator.writeTo(out);
out.writeVInt(slop);
if (out.getVersion().before(Version.V_6_0_0_rc1)) {
out.writeVInt(MatchQuery.DEFAULT_PHRASE_SLOP); // deprecated slop
}
out.writeVInt(prefixLength);
out.writeVInt(maxExpansions);
out.writeBoolean(fuzzyTranspositions);
Expand All @@ -165,34 +161,6 @@ public Object value() {
return this.value;
}

/**
* Sets the type of the text query.
*
* @deprecated Use {@link MatchPhraseQueryBuilder} for <code>phrase</code>
* queries and {@link MatchPhrasePrefixQueryBuilder} for
* <code>phrase_prefix</code> queries
*/
@Deprecated
public MatchQueryBuilder type(MatchQuery.Type type) {
if (type == null) {
throw new IllegalArgumentException("[" + NAME + "] requires type to be non-null");
}
this.type = type;
return this;
}

/**
* Get the type of the query.
*
* @deprecated Use {@link MatchPhraseQueryBuilder} for <code>phrase</code>
* queries and {@link MatchPhrasePrefixQueryBuilder} for
* <code>phrase_prefix</code> queries
*/
@Deprecated
public MatchQuery.Type type() {
return this.type;
}

/** Sets the operator to use when using a boolean query. Defaults to <tt>OR</tt>. */
public MatchQueryBuilder operator(Operator operator) {
if (operator == null) {
Expand Down Expand Up @@ -221,30 +189,6 @@ public String analyzer() {
return this.analyzer;
}

/**
* Sets a slop factor for phrase queries
*
* @deprecated for phrase queries use {@link MatchPhraseQueryBuilder}
*/
@Deprecated
public MatchQueryBuilder slop(int slop) {
if (slop < 0 ) {
throw new IllegalArgumentException("No negative slop allowed.");
}
this.slop = slop;
return this;
}

/**
* Get the slop factor for phrase queries.
*
* @deprecated for phrase queries use {@link MatchPhraseQueryBuilder}
*/
@Deprecated
public int slop() {
return this.slop;
}

/** Sets the fuzziness used when evaluated to a fuzzy query type. Defaults to "AUTO". */
public MatchQueryBuilder fuzziness(Object fuzziness) {
this.fuzziness = Fuzziness.build(fuzziness);
Expand Down Expand Up @@ -401,18 +345,10 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio
builder.startObject(fieldName);

builder.field(QUERY_FIELD.getPreferredName(), value);
// this is deprecated so only output the value if its not the default value (for bwc)
if (type != MatchQuery.Type.BOOLEAN) {
builder.field(TYPE_FIELD.getPreferredName(), type.toString().toLowerCase(Locale.ENGLISH));
}
builder.field(OPERATOR_FIELD.getPreferredName(), operator.toString());
if (analyzer != null) {
builder.field(ANALYZER_FIELD.getPreferredName(), analyzer);
}
// this is deprecated so only output the value if its not the default value (for bwc)
if (slop != MatchQuery.DEFAULT_PHRASE_SLOP) {
builder.field(SLOP_FIELD.getPreferredName(), slop);
}
if (fuzziness != null) {
fuzziness.toXContent(builder, params);
}
Expand Down Expand Up @@ -448,7 +384,6 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
if (analyzer != null) {
matchQuery.setAnalyzer(analyzer);
}
matchQuery.setPhraseSlop(slop);
matchQuery.setFuzziness(fuzziness);
matchQuery.setFuzzyPrefixLength(prefixLength);
matchQuery.setMaxExpansions(maxExpansions);
Expand All @@ -458,18 +393,16 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
matchQuery.setCommonTermsCutoff(cutoffFrequency);
matchQuery.setZeroTermsQuery(zeroTermsQuery);

Query query = matchQuery.parse(type, fieldName, value);
Query query = matchQuery.parse(MatchQuery.Type.BOOLEAN, fieldName, value);
return Queries.maybeApplyMinimumShouldMatch(query, minimumShouldMatch);
}

@Override
protected boolean doEquals(MatchQueryBuilder other) {
return Objects.equals(fieldName, other.fieldName) &&
Objects.equals(value, other.value) &&
Objects.equals(type, other.type) &&
Objects.equals(operator, other.operator) &&
Objects.equals(analyzer, other.analyzer) &&
Objects.equals(slop, other.slop) &&
Objects.equals(fuzziness, other.fuzziness) &&
Objects.equals(prefixLength, other.prefixLength) &&
Objects.equals(maxExpansions, other.maxExpansions) &&
Expand All @@ -483,7 +416,7 @@ protected boolean doEquals(MatchQueryBuilder other) {

@Override
protected int doHashCode() {
return Objects.hash(fieldName, value, type, operator, analyzer, slop,
return Objects.hash(fieldName, value, operator, analyzer,
fuzziness, prefixLength, maxExpansions, minimumShouldMatch,
fuzzyRewrite, lenient, fuzzyTranspositions, zeroTermsQuery, cutoffFrequency);
}
Expand All @@ -495,13 +428,11 @@ public String getWriteableName() {

public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOException {
String fieldName = null;
MatchQuery.Type type = MatchQuery.Type.BOOLEAN;
Object value = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String minimumShouldMatch = null;
String analyzer = null;
Operator operator = MatchQueryBuilder.DEFAULT_OPERATOR;
int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
Fuzziness fuzziness = null;
int prefixLength = FuzzyQuery.defaultPrefixLength;
int maxExpansion = FuzzyQuery.defaultMaxExpansions;
Expand All @@ -525,23 +456,10 @@ public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOExc
} else if (token.isValue()) {
if (QUERY_FIELD.match(currentFieldName)) {
value = parser.objectText();
} else if (TYPE_FIELD.match(currentFieldName)) {
String tStr = parser.text();
if ("boolean".equals(tStr)) {
type = MatchQuery.Type.BOOLEAN;
} else if ("phrase".equals(tStr)) {
type = MatchQuery.Type.PHRASE;
} else if ("phrase_prefix".equals(tStr) || ("phrasePrefix".equals(tStr))) {
type = MatchQuery.Type.PHRASE_PREFIX;
} else {
throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] query does not support type " + tStr);
}
} else if (ANALYZER_FIELD.match(currentFieldName)) {
analyzer = parser.text();
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName)) {
boost = parser.floatValue();
} else if (SLOP_FIELD.match(currentFieldName)) {
slop = parser.intValue();
} else if (Fuzziness.FIELD.match(currentFieldName)) {
fuzziness = Fuzziness.parse(parser);
} else if (PREFIX_LENGTH_FIELD.match(currentFieldName)) {
Expand Down Expand Up @@ -594,9 +512,7 @@ public static MatchQueryBuilder fromXContent(XContentParser parser) throws IOExc

MatchQueryBuilder matchQuery = new MatchQueryBuilder(fieldName, value);
matchQuery.operator(operator);
matchQuery.type(type);
matchQuery.analyzer(analyzer);
matchQuery.slop(slop);
matchQuery.minimumShouldMatch(minimumShouldMatch);
if (fuzziness != null) {
matchQuery.fuzziness(fuzziness);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.PointRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
Expand All @@ -40,7 +37,6 @@
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.search.MatchQuery;
import org.elasticsearch.index.search.MatchQuery.Type;
import org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.test.AbstractQueryTestCase;
Expand Down Expand Up @@ -144,23 +140,6 @@ protected void doAssertLuceneQuery(MatchQueryBuilder queryBuilder, Query query,
return;
}

switch (queryBuilder.type()) {
case BOOLEAN:
assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(ExtendedCommonTermsQuery.class))
.or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class)).or(instanceOf(MatchNoDocsQuery.class))
.or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
break;
case PHRASE:
assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(PhraseQuery.class))
.or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class))
.or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
break;
case PHRASE_PREFIX:
assertThat(query, either(instanceOf(BooleanQuery.class)).or(instanceOf(MultiPhrasePrefixQuery.class))
.or(instanceOf(TermQuery.class)).or(instanceOf(FuzzyQuery.class))
.or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
break;
}
QueryShardContext context = searchContext.getQueryShardContext();
MappedFieldType fieldType = context.fieldMapper(queryBuilder.fieldName());
if (query instanceof TermQuery && fieldType != null) {
Expand Down Expand Up @@ -246,11 +225,6 @@ public void testIllegalValues() {
assertEquals("[match] requires operator to be non-null", e.getMessage());
}

{
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> matchQuery.type(null));
assertEquals("[match] requires type to be non-null", e.getMessage());
}

{
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> matchQuery.zeroTermsQuery(null));
assertEquals("[match] requires zeroTermsQuery to be non-null", e.getMessage());
Expand Down Expand Up @@ -285,67 +259,6 @@ public void testSimpleMatchQuery() throws IOException {
assertEquals(json, Operator.AND, qb.operator());
}

public void testLegacyMatchPhrasePrefixQuery() throws IOException {
MatchQueryBuilder expectedQB = new MatchQueryBuilder("message", "to be or not to be");
expectedQB.type(Type.PHRASE_PREFIX);
expectedQB.slop(2);
expectedQB.maxExpansions(30);
String json = "{\n" +
" \"match\" : {\n" +
" \"message\" : {\n" +
" \"query\" : \"to be or not to be\",\n" +
" \"type\" : \"phrase_prefix\",\n" +
" \"operator\" : \"OR\",\n" +
" \"slop\" : 2,\n" +
" \"prefix_length\" : 0,\n" +
" \"max_expansions\" : 30,\n" +
" \"fuzzy_transpositions\" : true,\n" +
" \"lenient\" : false,\n" +
" \"zero_terms_query\" : \"NONE\",\n" +
" \"boost\" : 1.0\n" +
" }\n" +
" }\n" +
"}";
MatchQueryBuilder qb = (MatchQueryBuilder) parseQuery(json);
checkGeneratedJson(json, qb);

assertEquals(json, expectedQB, qb);

assertSerialization(qb);

assertWarnings("Deprecated field [type] used, replaced by [match_phrase and match_phrase_prefix query]",
"Deprecated field [slop] used, replaced by [match_phrase query]");
}

public void testLegacyMatchPhraseQuery() throws IOException {
MatchQueryBuilder expectedQB = new MatchQueryBuilder("message", "to be or not to be");
expectedQB.type(Type.PHRASE);
expectedQB.slop(2);
String json = "{\n" +
" \"match\" : {\n" +
" \"message\" : {\n" +
" \"query\" : \"to be or not to be\",\n" +
" \"type\" : \"phrase\",\n" +
" \"operator\" : \"OR\",\n" +
" \"slop\" : 2,\n" +
" \"prefix_length\" : 0,\n" +
" \"max_expansions\" : 50,\n" +
" \"fuzzy_transpositions\" : true,\n" +
" \"lenient\" : false,\n" +
" \"zero_terms_query\" : \"NONE\",\n" +
" \"boost\" : 1.0\n" +
" }\n" +
" }\n" +
"}";
MatchQueryBuilder qb = (MatchQueryBuilder) parseQuery(json);
checkGeneratedJson(json, qb);

assertEquals(json, expectedQB, qb);
assertSerialization(qb);
assertWarnings("Deprecated field [type] used, replaced by [match_phrase and match_phrase_prefix query]",
"Deprecated field [slop] used, replaced by [match_phrase query]");
}

public void testFuzzinessOnNonStringField() throws Exception {
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
MatchQueryBuilder query = new MatchQueryBuilder(INT_FIELD_NAME, 42);
Expand Down
Loading

0 comments on commit b63b023

Please sign in to comment.