From b63b023ec11c006aecf4daf98ae51021c82177d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Wed, 20 Sep 2017 14:24:30 +0200 Subject: [PATCH] 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. --- .../index/query/MatchQueryBuilder.java | 114 +++--------------- .../index/query/MatchQueryBuilderTests.java | 87 ------------- .../highlight/HighlighterSearchIT.java | 15 ++- .../search/query/MultiMatchQueryIT.java | 12 +- .../search/query/SearchQueryIT.java | 17 +-- .../migration/migrate_6_0/search.asciidoc | 4 + .../script/mustache/SearchTemplateIT.java | 17 ++- 7 files changed, 48 insertions(+), 218 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java index edd444aae430c..9ca8d015f7747 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java @@ -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; @@ -34,7 +35,6 @@ import org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery; import java.io.IOException; -import java.util.Locale; import java.util.Objects; /** @@ -42,7 +42,6 @@ * result of the analysis. */ public class MatchQueryBuilder extends AbstractQueryBuilder { - 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"); @@ -53,7 +52,6 @@ public class MatchQueryBuilder extends AbstractQueryBuilder { 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 */ @@ -62,24 +60,14 @@ public class MatchQueryBuilder extends AbstractQueryBuilder { /** 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; @@ -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(); @@ -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); @@ -165,34 +161,6 @@ public Object value() { return this.value; } - /** - * Sets the type of the text query. - * - * @deprecated Use {@link MatchPhraseQueryBuilder} for phrase - * queries and {@link MatchPhrasePrefixQueryBuilder} for - * phrase_prefix 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 phrase - * queries and {@link MatchPhrasePrefixQueryBuilder} for - * phrase_prefix queries - */ - @Deprecated - public MatchQuery.Type type() { - return this.type; - } - /** Sets the operator to use when using a boolean query. Defaults to OR. */ public MatchQueryBuilder operator(Operator operator) { if (operator == null) { @@ -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); @@ -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); } @@ -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); @@ -458,7 +393,7 @@ 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); } @@ -466,10 +401,8 @@ protected Query doToQuery(QueryShardContext context) throws IOException { 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) && @@ -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); } @@ -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; @@ -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)) { @@ -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); diff --git a/core/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java index 31687c5d9ff59..2aaf214d1d57a 100644 --- a/core/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java @@ -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; @@ -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; @@ -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) { @@ -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()); @@ -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); diff --git a/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java b/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java index 22803259faa05..1d752aab17545 100644 --- a/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java +++ b/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java @@ -19,6 +19,7 @@ package org.elasticsearch.search.fetch.subphase.highlight; import com.carrotsearch.randomizedtesting.generators.RandomPicks; + import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.search.SearchRequestBuilder; @@ -39,7 +40,6 @@ import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; import org.elasticsearch.index.query.functionscore.RandomScoreFunctionBuilder; -import org.elasticsearch.index.search.MatchQuery; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; @@ -79,7 +79,6 @@ import static org.elasticsearch.index.query.QueryBuilders.rangeQuery; import static org.elasticsearch.index.query.QueryBuilders.regexpQuery; import static org.elasticsearch.index.query.QueryBuilders.termQuery; -import static org.elasticsearch.index.query.QueryBuilders.typeQuery; import static org.elasticsearch.index.query.QueryBuilders.wildcardQuery; import static org.elasticsearch.search.builder.SearchSourceBuilder.highlight; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; @@ -1474,7 +1473,7 @@ public void testPlainHighlightDifferentFragmenter() throws Exception { refresh(); SearchResponse response = client().prepareSearch("test") - .setQuery(QueryBuilders.matchQuery("tags", "long tag").type(MatchQuery.Type.PHRASE)) + .setQuery(QueryBuilders.matchPhraseQuery("tags", "long tag")) .highlighter( new HighlightBuilder().field(new HighlightBuilder.Field("tags") .highlighterType("plain").fragmentSize(-1).numOfFragments(2).fragmenter("simple"))) @@ -1485,7 +1484,7 @@ public void testPlainHighlightDifferentFragmenter() throws Exception { equalTo("here is another one that is very long tag and has the tag token near the end")); response = client().prepareSearch("test") - .setQuery(QueryBuilders.matchQuery("tags", "long tag").type(MatchQuery.Type.PHRASE)) + .setQuery(QueryBuilders.matchPhraseQuery("tags", "long tag")) .highlighter( new HighlightBuilder().field(new Field("tags").highlighterType("plain").fragmentSize(-1).numOfFragments(2) .fragmenter("span"))).get(); @@ -1496,7 +1495,7 @@ public void testPlainHighlightDifferentFragmenter() throws Exception { equalTo("here is another one that is very long tag and has the tag token near the end")); assertFailures(client().prepareSearch("test") - .setQuery(QueryBuilders.matchQuery("tags", "long tag").type(MatchQuery.Type.PHRASE)) + .setQuery(QueryBuilders.matchPhraseQuery("tags", "long tag")) .highlighter( new HighlightBuilder().field(new Field("tags").highlighterType("plain").fragmentSize(-1).numOfFragments(2) .fragmenter("invalid"))), @@ -1554,7 +1553,7 @@ public void testMissingStoredField() throws Exception { // This query used to fail when the field to highlight was absent SearchResponse response = client().prepareSearch("test") - .setQuery(QueryBuilders.matchQuery("field", "highlight").type(MatchQuery.Type.BOOLEAN)) + .setQuery(QueryBuilders.matchQuery("field", "highlight")) .highlighter( new HighlightBuilder().field(new HighlightBuilder.Field("highlight_field").fragmentSize(-1).numOfFragments(1) .fragmenter("simple"))).get(); @@ -1579,7 +1578,7 @@ public void testNumericHighlighting() throws Exception { refresh(); SearchResponse response = client().prepareSearch("test") - .setQuery(QueryBuilders.matchQuery("text", "test").type(MatchQuery.Type.BOOLEAN)) + .setQuery(QueryBuilders.matchQuery("text", "test")) .highlighter( new HighlightBuilder().field("text").field("byte").field("short").field("int").field("long").field("float") .field("double")) @@ -1604,7 +1603,7 @@ public void testResetTwice() throws Exception { refresh(); SearchResponse response = client().prepareSearch("test") - .setQuery(QueryBuilders.matchQuery("text", "test").type(MatchQuery.Type.BOOLEAN)) + .setQuery(QueryBuilders.matchQuery("text", "test")) .highlighter(new HighlightBuilder().field("text")).execute().actionGet(); // PatternAnalyzer will throw an exception if it is resetted twice assertHitCount(response, 1L); diff --git a/core/src/test/java/org/elasticsearch/search/query/MultiMatchQueryIT.java b/core/src/test/java/org/elasticsearch/search/query/MultiMatchQueryIT.java index ba7a13cf0102e..37ffda5f46a0f 100644 --- a/core/src/test/java/org/elasticsearch/search/query/MultiMatchQueryIT.java +++ b/core/src/test/java/org/elasticsearch/search/query/MultiMatchQueryIT.java @@ -179,7 +179,7 @@ private XContentBuilder createMapping() throws IOException { } public void testDefaults() throws ExecutionException, InterruptedException { - MatchQuery.Type type = randomBoolean() ? MatchQueryBuilder.DEFAULT_TYPE : MatchQuery.Type.BOOLEAN; + MatchQuery.Type type = MatchQuery.Type.BOOLEAN; SearchResponse searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("marvel hero captain america", "full_name", "first_name", "last_name", "category") .operator(Operator.OR))).get(); @@ -270,9 +270,7 @@ public void testSingleField() throws NoSuchFieldException, IllegalAccessExceptio .addSort("_id", SortOrder.ASC) .setQuery(multiMatchQueryBuilder).get(); MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(field, builder.toString()); - if (multiMatchQueryBuilder.getType() != null) { - matchQueryBuilder.type(MatchQuery.Type.valueOf(multiMatchQueryBuilder.getType().matchQueryType().toString())); - } + SearchResponse matchResp = client().prepareSearch("test") // _id tie sort .addSort("_score", SortOrder.DESC) @@ -294,7 +292,7 @@ public void testSingleField() throws NoSuchFieldException, IllegalAccessExceptio public void testCutoffFreq() throws ExecutionException, InterruptedException { final long numDocs = client().prepareSearch("test").setSize(0) .setQuery(matchAllQuery()).get().getHits().getTotalHits(); - MatchQuery.Type type = randomBoolean() ? MatchQueryBuilder.DEFAULT_TYPE : MatchQuery.Type.BOOLEAN; + MatchQuery.Type type = MatchQuery.Type.BOOLEAN; Float cutoffFrequency = randomBoolean() ? Math.min(1, numDocs * 1.f / between(10, 20)) : 1.f / between(10, 20); SearchResponse searchResponse = client().prepareSearch("test") .setQuery(randomizeType(multiMatchQuery("marvel hero captain america", "full_name", "first_name", "last_name", "category") @@ -357,7 +355,7 @@ public void testEquivalence() { int numIters = scaledRandomIntBetween(5, 10); for (int i = 0; i < numIters; i++) { { - MatchQuery.Type type = randomBoolean() ? MatchQueryBuilder.DEFAULT_TYPE : MatchQuery.Type.BOOLEAN; + MatchQuery.Type type = MatchQuery.Type.BOOLEAN; MultiMatchQueryBuilder multiMatchQueryBuilder = randomBoolean() ? multiMatchQuery("marvel hero captain america", "full_name", "first_name", "last_name", "category") : multiMatchQuery("marvel hero captain america", "*_name", randomBoolean() ? "category" : "categ*"); SearchResponse left = client().prepareSearch("test").setSize(numDocs) @@ -377,7 +375,7 @@ public void testEquivalence() { } { - MatchQuery.Type type = randomBoolean() ? MatchQueryBuilder.DEFAULT_TYPE : MatchQuery.Type.BOOLEAN; + MatchQuery.Type type = MatchQuery.Type.BOOLEAN; String minShouldMatch = randomBoolean() ? null : "" + between(0, 1); Operator op = randomBoolean() ? Operator.AND : Operator.OR; MultiMatchQueryBuilder multiMatchQueryBuilder = randomBoolean() ? multiMatchQuery("captain america", "full_name", "first_name", "last_name", "category") : diff --git a/core/src/test/java/org/elasticsearch/search/query/SearchQueryIT.java b/core/src/test/java/org/elasticsearch/search/query/SearchQueryIT.java index 06f6e20d3836f..a9ab6ed0cda4f 100644 --- a/core/src/test/java/org/elasticsearch/search/query/SearchQueryIT.java +++ b/core/src/test/java/org/elasticsearch/search/query/SearchQueryIT.java @@ -40,7 +40,6 @@ import org.elasticsearch.index.query.WrapperQueryBuilder; import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders; import org.elasticsearch.index.search.MatchQuery; -import org.elasticsearch.index.search.MatchQuery.Type; import org.elasticsearch.indices.TermsLookup; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.rest.RestStatus; @@ -70,6 +69,8 @@ import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery; import static org.elasticsearch.index.query.QueryBuilders.idsQuery; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; +import static org.elasticsearch.index.query.QueryBuilders.matchPhrasePrefixQuery; +import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery; import static org.elasticsearch.index.query.QueryBuilders.matchQuery; import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery; import static org.elasticsearch.index.query.QueryBuilders.prefixQuery; @@ -99,7 +100,6 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThirdHit; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasScore; -import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.closeTo; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; @@ -173,10 +173,10 @@ public void testIndexOptions() throws Exception { client().prepareIndex("test", "type1", "1").setSource("field1", "quick brown fox", "field2", "quick brown fox"), client().prepareIndex("test", "type1", "2").setSource("field1", "quick lazy huge brown fox", "field2", "quick lazy huge brown fox")); - SearchResponse searchResponse = client().prepareSearch().setQuery(matchQuery("field2", "quick brown").type(Type.PHRASE).slop(0)).get(); + SearchResponse searchResponse = client().prepareSearch().setQuery(matchPhraseQuery("field2", "quick brown").slop(0)).get(); assertHitCount(searchResponse, 1L); - assertFailures(client().prepareSearch().setQuery(matchQuery("field1", "quick brown").type(Type.PHRASE).slop(0)), + assertFailures(client().prepareSearch().setQuery(matchPhraseQuery("field1", "quick brown").slop(0)), RestStatus.INTERNAL_SERVER_ERROR, containsString("field \"field1\" was indexed without position data; cannot run PhraseQuery")); } @@ -1407,7 +1407,7 @@ public void testMustNot() throws IOException, ExecutionException, InterruptedExc searchResponse = client().prepareSearch("test").setQuery( boolQuery() - .mustNot(matchQuery("description", "anything").type(Type.BOOLEAN)) + .mustNot(matchQuery("description", "anything")) ).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).get(); assertHitCount(searchResponse, 2L); } @@ -1859,13 +1859,14 @@ public void testMatchPhrasePrefixQuery() throws ExecutionException, InterruptedE client().prepareIndex("test1", "type1", "2").setSource("field", "trying out Elasticsearch")); - SearchResponse searchResponse = client().prepareSearch().setQuery(matchQuery("field", "Johnnie la").slop(between(2,5)).type(Type.PHRASE_PREFIX)).get(); + SearchResponse searchResponse = client().prepareSearch().setQuery(matchPhrasePrefixQuery("field", "Johnnie la").slop(between(2, 5))) + .get(); assertHitCount(searchResponse, 1L); assertSearchHits(searchResponse, "1"); - searchResponse = client().prepareSearch().setQuery(matchQuery("field", "trying").type(Type.PHRASE_PREFIX)).get(); + searchResponse = client().prepareSearch().setQuery(matchPhrasePrefixQuery("field", "trying")).get(); assertHitCount(searchResponse, 1L); assertSearchHits(searchResponse, "2"); - searchResponse = client().prepareSearch().setQuery(matchQuery("field", "try").type(Type.PHRASE_PREFIX)).get(); + searchResponse = client().prepareSearch().setQuery(matchPhrasePrefixQuery("field", "try")).get(); assertHitCount(searchResponse, 1L); assertSearchHits(searchResponse, "2"); } diff --git a/docs/reference/migration/migrate_6_0/search.asciidoc b/docs/reference/migration/migrate_6_0/search.asciidoc index cc81eff7764ce..812a9088f10d1 100644 --- a/docs/reference/migration/migrate_6_0/search.asciidoc +++ b/docs/reference/migration/migrate_6_0/search.asciidoc @@ -76,6 +76,10 @@ now mandatory. Previously, the index defaulted to the index the query was executed on. Now this index must be explicitly set in the request. +* The deprecated `type` and `slop` parameter for the `match` query have been removed. Instead of +setting the `type`, the `match_phrase` or `match_phrase_prefix` should be used. The `slop` removed from +the `match` query but is supported for `match_phrase` and `match_phrase_prefix`. + ==== Search shards API The search shards API no longer accepts the `type` url parameter, which didn't diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java index 39657bc177736..69739ff2cb8ef 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/SearchTemplateIT.java @@ -22,7 +22,6 @@ import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse; import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.search.SearchRequest; -import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; @@ -283,8 +282,10 @@ public void testIndexedTemplateOverwrite() throws Exception { for (int i = 1; i < iterations; i++) { assertAcked(client().admin().cluster().preparePutStoredScript() .setId("git01") - .setContent(new BytesArray("{\"template\":{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\"," + - "\"type\": \"ooophrase_prefix\"}}}}}"), XContentType.JSON)); + .setContent(new BytesArray( + "{\"template\":{\"query\": {\"match_phrase_prefix\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\"," + + "\"slop\": -1}}}}}"), + XContentType.JSON)); GetStoredScriptResponse getResponse = client().admin().cluster().prepareGetStoredScript("git01").get(); assertNotNull(getResponse.getSource()); @@ -292,24 +293,22 @@ public void testIndexedTemplateOverwrite() throws Exception { Map templateParams = new HashMap<>(); templateParams.put("P_Keyword1", "dev"); - ParsingException e = expectThrows(ParsingException.class, () -> new SearchTemplateRequestBuilder(client()) + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new SearchTemplateRequestBuilder(client()) .setRequest(new SearchRequest("testindex").types("test")) .setScript("git01").setScriptType(ScriptType.STORED).setScriptParams(templateParams) .get()); - assertThat(e.getMessage(), containsString("[match] query does not support type ooophrase_prefix")); - assertWarnings("Deprecated field [type] used, replaced by [match_phrase and match_phrase_prefix query]"); + assertThat(e.getMessage(), containsString("No negative slop allowed")); assertAcked(client().admin().cluster().preparePutStoredScript() .setId("git01") - .setContent(new BytesArray("{\"query\": {\"match\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\"," + - "\"type\": \"phrase_prefix\"}}}}"), XContentType.JSON)); + .setContent(new BytesArray("{\"query\": {\"match_phrase_prefix\": {\"searchtext\": {\"query\": \"{{P_Keyword1}}\"," + + "\"slop\": 0}}}}"), XContentType.JSON)); SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client()) .setRequest(new SearchRequest("testindex").types("test")) .setScript("git01").setScriptType(ScriptType.STORED).setScriptParams(templateParams) .get(); assertHitCount(searchResponse.getResponse(), 1); - assertWarnings("Deprecated field [type] used, replaced by [match_phrase and match_phrase_prefix query]"); } }