diff --git a/server/src/main/java/org/elasticsearch/index/query/CombinedFieldsQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/CombinedFieldsQueryBuilder.java index 55d9e271054e9..52741a88e056d 100644 --- a/server/src/main/java/org/elasticsearch/index/query/CombinedFieldsQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/CombinedFieldsQueryBuilder.java @@ -59,12 +59,16 @@ public class CombinedFieldsQueryBuilder extends AbstractQueryBuilder fieldsAndBoosts; - private Operator operator = Operator.OR; + private Operator operator = DEFAULT_OPERATOR; private String minimumShouldMatch; - private ZeroTermsQueryOption zeroTermsQuery = ZeroTermsQueryOption.NONE; - private boolean autoGenerateSynonymsPhraseQuery = true; + private ZeroTermsQueryOption zeroTermsQuery = DEFAULT_ZERO_TERMS_QUERY; + private boolean autoGenerateSynonymsPhraseQuery = DEFAULT_GENERATE_SYNONYMS_PHRASE; private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( NAME, @@ -257,13 +261,19 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio builder.value(fieldEntry.getKey() + "^" + fieldEntry.getValue()); } builder.endArray(); - builder.field(OPERATOR_FIELD.getPreferredName(), operator.toString()); + if (operator != DEFAULT_OPERATOR) { + builder.field(OPERATOR_FIELD.getPreferredName(), operator.toString()); + } if (minimumShouldMatch != null) { builder.field(MINIMUM_SHOULD_MATCH_FIELD.getPreferredName(), minimumShouldMatch); } - builder.field(ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString()); - builder.field(GENERATE_SYNONYMS_PHRASE_QUERY.getPreferredName(), autoGenerateSynonymsPhraseQuery); - printBoostAndQueryName(builder); + if (zeroTermsQuery != DEFAULT_ZERO_TERMS_QUERY) { + builder.field(ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString()); + } + if (autoGenerateSynonymsPhraseQuery != DEFAULT_GENERATE_SYNONYMS_PHRASE) { + builder.field(GENERATE_SYNONYMS_PHRASE_QUERY.getPreferredName(), autoGenerateSynonymsPhraseQuery); + } + boostAndQueryNameToXContent(builder); builder.endObject(); } diff --git a/server/src/main/java/org/elasticsearch/index/query/IntervalQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/IntervalQueryBuilder.java index dcc55fe8e661c..3b1f16ad6334f 100644 --- a/server/src/main/java/org/elasticsearch/index/query/IntervalQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/IntervalQueryBuilder.java @@ -65,7 +65,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep builder.field(field); builder.startObject(); sourceProvider.toXContent(builder, params); - printBoostAndQueryName(builder); + boostAndQueryNameToXContent(builder); builder.endObject(); builder.endObject(); } diff --git a/server/src/main/java/org/elasticsearch/index/query/MatchBoolPrefixQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/MatchBoolPrefixQueryBuilder.java index 286192a19566f..f06e502ac4c16 100644 --- a/server/src/main/java/org/elasticsearch/index/query/MatchBoolPrefixQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/MatchBoolPrefixQueryBuilder.java @@ -238,20 +238,28 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep if (analyzer != null) { builder.field(MatchQueryBuilder.ANALYZER_FIELD.getPreferredName(), analyzer); } - builder.field(OPERATOR_FIELD.getPreferredName(), operator.toString()); + if (operator != DEFAULT_OPERATOR) { + builder.field(OPERATOR_FIELD.getPreferredName(), operator.toString()); + } if (minimumShouldMatch != null) { builder.field(MatchQueryBuilder.MINIMUM_SHOULD_MATCH_FIELD.getPreferredName(), minimumShouldMatch); } if (fuzziness != null) { fuzziness.toXContent(builder, params); } - builder.field(PREFIX_LENGTH_FIELD.getPreferredName(), prefixLength); - builder.field(MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions); - builder.field(FUZZY_TRANSPOSITIONS_FIELD.getPreferredName(), fuzzyTranspositions); + if (prefixLength != FuzzyQuery.defaultPrefixLength) { + builder.field(PREFIX_LENGTH_FIELD.getPreferredName(), prefixLength); + } + if (maxExpansions != FuzzyQuery.defaultMaxExpansions) { + builder.field(MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions); + } + if (fuzzyTranspositions != FuzzyQuery.defaultTranspositions) { + builder.field(FUZZY_TRANSPOSITIONS_FIELD.getPreferredName(), fuzzyTranspositions); + } if (fuzzyRewrite != null) { builder.field(FUZZY_REWRITE_FIELD.getPreferredName(), fuzzyRewrite); } - printBoostAndQueryName(builder); + boostAndQueryNameToXContent(builder); builder.endObject(); builder.endObject(); } diff --git a/server/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java index 98ec2d74ce60e..23ef97e1cd593 100644 --- a/server/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilder.java @@ -169,10 +169,16 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep if (analyzer != null) { builder.field(MatchQueryBuilder.ANALYZER_FIELD.getPreferredName(), analyzer); } - builder.field(MatchPhraseQueryBuilder.SLOP_FIELD.getPreferredName(), slop); - builder.field(MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions); - builder.field(ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString()); - printBoostAndQueryName(builder); + if (slop != MatchQueryParser.DEFAULT_PHRASE_SLOP) { + builder.field(MatchPhraseQueryBuilder.SLOP_FIELD.getPreferredName(), slop); + } + if (maxExpansions != FuzzyQuery.defaultMaxExpansions) { + builder.field(MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions); + } + if (zeroTermsQuery != MatchQueryParser.DEFAULT_ZERO_TERMS_QUERY) { + builder.field(ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString()); + } + boostAndQueryNameToXContent(builder); builder.endObject(); builder.endObject(); } diff --git a/server/src/main/java/org/elasticsearch/index/query/MatchPhraseQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/MatchPhraseQueryBuilder.java index 7eeb7e43048a3..3fbfc1ce0f694 100644 --- a/server/src/main/java/org/elasticsearch/index/query/MatchPhraseQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/MatchPhraseQueryBuilder.java @@ -145,9 +145,13 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep if (analyzer != null) { builder.field(MatchQueryBuilder.ANALYZER_FIELD.getPreferredName(), analyzer); } - builder.field(SLOP_FIELD.getPreferredName(), slop); - builder.field(ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString()); - printBoostAndQueryName(builder); + if (slop != MatchQueryParser.DEFAULT_PHRASE_SLOP) { + builder.field(SLOP_FIELD.getPreferredName(), slop); + } + if (zeroTermsQuery != MatchQueryParser.DEFAULT_ZERO_TERMS_QUERY) { + builder.field(ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString()); + } + boostAndQueryNameToXContent(builder); builder.endObject(); builder.endObject(); } diff --git a/server/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java index dc489a093452a..c8a7c2cd0ad27 100644 --- a/server/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java @@ -527,17 +527,27 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio builder.value(fieldEntry.getKey() + "^" + fieldEntry.getValue()); } builder.endArray(); - builder.field(TYPE_FIELD.getPreferredName(), type.toString().toLowerCase(Locale.ENGLISH)); - builder.field(OPERATOR_FIELD.getPreferredName(), operator.toString()); + if (type != DEFAULT_TYPE) { + builder.field(TYPE_FIELD.getPreferredName(), type.toString().toLowerCase(Locale.ENGLISH)); + } + if (operator != DEFAULT_OPERATOR) { + builder.field(OPERATOR_FIELD.getPreferredName(), operator.toString()); + } if (analyzer != null) { builder.field(ANALYZER_FIELD.getPreferredName(), analyzer); } - builder.field(SLOP_FIELD.getPreferredName(), slop); + if (slop != DEFAULT_PHRASE_SLOP) { + builder.field(SLOP_FIELD.getPreferredName(), slop); + } if (fuzziness != null) { fuzziness.toXContent(builder, params); } - builder.field(PREFIX_LENGTH_FIELD.getPreferredName(), prefixLength); - builder.field(MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions); + if (prefixLength != DEFAULT_PREFIX_LENGTH) { + builder.field(PREFIX_LENGTH_FIELD.getPreferredName(), prefixLength); + } + if (maxExpansions != DEFAULT_MAX_EXPANSIONS) { + builder.field(MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions); + } if (minimumShouldMatch != null) { builder.field(MINIMUM_SHOULD_MATCH_FIELD.getPreferredName(), minimumShouldMatch); } @@ -550,10 +560,16 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio if (lenient != null) { builder.field(LENIENT_FIELD.getPreferredName(), lenient); } - builder.field(ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString()); - builder.field(GENERATE_SYNONYMS_PHRASE_QUERY.getPreferredName(), autoGenerateSynonymsPhraseQuery); - builder.field(FUZZY_TRANSPOSITIONS_FIELD.getPreferredName(), fuzzyTranspositions); - printBoostAndQueryName(builder); + if (zeroTermsQuery != DEFAULT_ZERO_TERMS_QUERY) { + builder.field(ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString()); + } + if (autoGenerateSynonymsPhraseQuery != true) { + builder.field(GENERATE_SYNONYMS_PHRASE_QUERY.getPreferredName(), autoGenerateSynonymsPhraseQuery); + } + if (fuzzyTranspositions != DEFAULT_FUZZY_TRANSPOSITIONS) { + builder.field(FUZZY_TRANSPOSITIONS_FIELD.getPreferredName(), fuzzyTranspositions); + } + boostAndQueryNameToXContent(builder); builder.endObject(); } diff --git a/server/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java index d41cce04b4268..90c52e37b4e78 100644 --- a/server/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/QueryStringQueryBuilder.java @@ -571,31 +571,45 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep builder.value(fieldEntry.getKey() + "^" + fieldEntry.getValue()); } builder.endArray(); - if (this.type != null) { + if (this.type != DEFAULT_TYPE) { builder.field(TYPE_FIELD.getPreferredName(), type.toString().toLowerCase(Locale.ENGLISH)); } if (tieBreaker != null) { builder.field(TIE_BREAKER_FIELD.getPreferredName(), this.tieBreaker); } - builder.field(DEFAULT_OPERATOR_FIELD.getPreferredName(), this.defaultOperator.name().toLowerCase(Locale.ROOT)); + if (defaultOperator != DEFAULT_OPERATOR) { + builder.field(DEFAULT_OPERATOR_FIELD.getPreferredName(), this.defaultOperator.name().toLowerCase(Locale.ROOT)); + } if (this.analyzer != null) { builder.field(ANALYZER_FIELD.getPreferredName(), this.analyzer); } if (this.quoteAnalyzer != null) { builder.field(QUOTE_ANALYZER_FIELD.getPreferredName(), this.quoteAnalyzer); } - builder.field(MAX_DETERMINIZED_STATES_FIELD.getPreferredName(), this.maxDeterminizedStates); + if (this.maxDeterminizedStates != DEFAULT_MAX_DETERMINED_STATES) { + builder.field(MAX_DETERMINIZED_STATES_FIELD.getPreferredName(), this.maxDeterminizedStates); + } if (this.allowLeadingWildcard != null) { builder.field(ALLOW_LEADING_WILDCARD_FIELD.getPreferredName(), this.allowLeadingWildcard); } - builder.field(ENABLE_POSITION_INCREMENTS_FIELD.getPreferredName(), this.enablePositionIncrements); - this.fuzziness.toXContent(builder, params); - builder.field(FUZZY_PREFIX_LENGTH_FIELD.getPreferredName(), this.fuzzyPrefixLength); - builder.field(FUZZY_MAX_EXPANSIONS_FIELD.getPreferredName(), this.fuzzyMaxExpansions); + if (this.enablePositionIncrements != DEFAULT_ENABLE_POSITION_INCREMENTS) { + builder.field(ENABLE_POSITION_INCREMENTS_FIELD.getPreferredName(), this.enablePositionIncrements); + } + if (this.fuzziness != DEFAULT_FUZZINESS) { + this.fuzziness.toXContent(builder, params); + } + if (this.fuzzyPrefixLength != DEFAULT_FUZZY_PREFIX_LENGTH) { + builder.field(FUZZY_PREFIX_LENGTH_FIELD.getPreferredName(), this.fuzzyPrefixLength); + } + if (this.fuzzyMaxExpansions != DEFAULT_FUZZY_MAX_EXPANSIONS) { + builder.field(FUZZY_MAX_EXPANSIONS_FIELD.getPreferredName(), this.fuzzyMaxExpansions); + } if (this.fuzzyRewrite != null) { builder.field(FUZZY_REWRITE_FIELD.getPreferredName(), this.fuzzyRewrite); } - builder.field(PHRASE_SLOP_FIELD.getPreferredName(), this.phraseSlop); + if (this.phraseSlop != DEFAULT_PHRASE_SLOP) { + builder.field(PHRASE_SLOP_FIELD.getPreferredName(), this.phraseSlop); + } if (this.analyzeWildcard != null) { builder.field(ANALYZE_WILDCARD_FIELD.getPreferredName(), this.analyzeWildcard); } @@ -614,10 +628,16 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep if (this.timeZone != null) { builder.field(TIME_ZONE_FIELD.getPreferredName(), this.timeZone.getId()); } - builder.field(ESCAPE_FIELD.getPreferredName(), this.escape); - builder.field(GENERATE_SYNONYMS_PHRASE_QUERY.getPreferredName(), autoGenerateSynonymsPhraseQuery); - builder.field(FUZZY_TRANSPOSITIONS_FIELD.getPreferredName(), fuzzyTranspositions); - printBoostAndQueryName(builder); + if (this.escape != DEFAULT_ESCAPE) { + builder.field(ESCAPE_FIELD.getPreferredName(), this.escape); + } + if (this.autoGenerateSynonymsPhraseQuery != true) { + builder.field(GENERATE_SYNONYMS_PHRASE_QUERY.getPreferredName(), autoGenerateSynonymsPhraseQuery); + } + if (this.fuzzyTranspositions != DEFAULT_FUZZY_TRANSPOSITIONS) { + builder.field(FUZZY_TRANSPOSITIONS_FIELD.getPreferredName(), fuzzyTranspositions); + } + boostAndQueryNameToXContent(builder); builder.endObject(); } diff --git a/server/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java b/server/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java index d391cb33f39a0..c26675f50060f 100644 --- a/server/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/SimpleQueryStringBuilder.java @@ -433,12 +433,18 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep builder.field(ANALYZER_FIELD.getPreferredName(), analyzer); } - builder.field(FLAGS_FIELD.getPreferredName(), flags); - builder.field(DEFAULT_OPERATOR_FIELD.getPreferredName(), defaultOperator.name().toLowerCase(Locale.ROOT)); + if (flags != DEFAULT_FLAGS) { + builder.field(FLAGS_FIELD.getPreferredName(), flags); + } + if (defaultOperator != DEFAULT_OPERATOR) { + builder.field(DEFAULT_OPERATOR_FIELD.getPreferredName(), defaultOperator.name().toLowerCase(Locale.ROOT)); + } if (lenientSet) { builder.field(LENIENT_FIELD.getPreferredName(), settings.lenient()); } - builder.field(ANALYZE_WILDCARD_FIELD.getPreferredName(), settings.analyzeWildcard()); + if (settings.analyzeWildcard() != DEFAULT_ANALYZE_WILDCARD) { + builder.field(ANALYZE_WILDCARD_FIELD.getPreferredName(), settings.analyzeWildcard()); + } if (settings.quoteFieldSuffix() != null) { builder.field(QUOTE_FIELD_SUFFIX_FIELD.getPreferredName(), settings.quoteFieldSuffix()); } @@ -446,11 +452,19 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep if (minimumShouldMatch != null) { builder.field(MINIMUM_SHOULD_MATCH_FIELD.getPreferredName(), minimumShouldMatch); } - builder.field(GENERATE_SYNONYMS_PHRASE_QUERY.getPreferredName(), settings.autoGenerateSynonymsPhraseQuery()); - builder.field(FUZZY_PREFIX_LENGTH_FIELD.getPreferredName(), settings.fuzzyPrefixLength()); - builder.field(FUZZY_MAX_EXPANSIONS_FIELD.getPreferredName(), settings.fuzzyMaxExpansions()); - builder.field(FUZZY_TRANSPOSITIONS_FIELD.getPreferredName(), settings.fuzzyTranspositions()); - printBoostAndQueryName(builder); + if (settings.autoGenerateSynonymsPhraseQuery() != true) { + builder.field(GENERATE_SYNONYMS_PHRASE_QUERY.getPreferredName(), settings.autoGenerateSynonymsPhraseQuery()); + } + if (settings.fuzzyPrefixLength() != DEFAULT_FUZZY_PREFIX_LENGTH) { + builder.field(FUZZY_PREFIX_LENGTH_FIELD.getPreferredName(), settings.fuzzyPrefixLength()); + } + if (settings.fuzzyMaxExpansions() != DEFAULT_FUZZY_MAX_EXPANSIONS) { + builder.field(FUZZY_MAX_EXPANSIONS_FIELD.getPreferredName(), settings.fuzzyMaxExpansions()); + } + if (settings.fuzzyTranspositions() != DEFAULT_FUZZY_TRANSPOSITIONS) { + builder.field(FUZZY_TRANSPOSITIONS_FIELD.getPreferredName(), settings.fuzzyTranspositions()); + } + boostAndQueryNameToXContent(builder); builder.endObject(); } diff --git a/server/src/test/java/org/elasticsearch/index/query/CombinedFieldsQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/CombinedFieldsQueryBuilderTests.java index 78d70d7c2fcd9..74f0ec2bd437a 100644 --- a/server/src/test/java/org/elasticsearch/index/query/CombinedFieldsQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/CombinedFieldsQueryBuilderTests.java @@ -79,9 +79,9 @@ public void testValuesFromXContent() throws IOException { "combined_fields" : { "query" : "quick brown fox", "fields" : [ "abstract^1.0", "body^1.0", "title^1.0" ], - "operator" : "OR", - "zero_terms_query" : "NONE", - "auto_generate_synonyms_phrase_query" : true, + "operator" : "AND", + "zero_terms_query" : "ALL", + "auto_generate_synonyms_phrase_query" : false, "boost" : 2.0 } }"""; @@ -91,7 +91,7 @@ public void testValuesFromXContent() throws IOException { assertEquals(json, "quick brown fox", parsed.value()); assertEquals(json, 3, parsed.fields().size()); - assertEquals(json, Operator.OR, parsed.operator()); + assertEquals(json, Operator.AND, parsed.operator()); assertEquals(json, 2.0, parsed.boost, 1e-6); } diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchBoolPrefixQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchBoolPrefixQueryBuilderTests.java index e9a6b99a10a54..0544498f72296 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchBoolPrefixQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchBoolPrefixQueryBuilderTests.java @@ -161,12 +161,7 @@ public void testFromSimpleJson() throws IOException { { "match_bool_prefix": { "fieldName": { - "query": "fieldValue", - "operator": "OR", - "prefix_length": 0, - "max_expansions": 50, - "fuzzy_transpositions": true, - "boost": 1.0 + "query": "fieldValue" } } }"""; diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java index 00de0a11bb0ce..fc21a25bfde15 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java @@ -141,11 +141,7 @@ public void testPhrasePrefixMatchQuery() throws IOException { { "match_phrase_prefix" : { "message" : { - "query" : "this is a test", - "slop" : 0, - "max_expansions" : 50, - "zero_terms_query" : "NONE", - "boost" : 1.0 + "query" : "this is a test" } } }"""; @@ -166,10 +162,7 @@ public void testPhrasePrefixMatchQuery() throws IOException { "match_phrase_prefix" : { "message" : { "query" : "this is a test", - "slop" : 0, - "max_expansions" : 10, - "zero_terms_query" : "NONE", - "boost" : 1.0 + "max_expansions" : 10 } } }"""; diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchPhraseQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchPhraseQueryBuilderTests.java index 8ed1882c6743d..841dae9b11887 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchPhraseQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchPhraseQueryBuilderTests.java @@ -134,10 +134,7 @@ public void testFromSimpleJson() throws IOException { { "match_phrase" : { "message" : { - "query" : "this is a test", - "slop" : 0, - "zero_terms_query" : "NONE", - "boost" : 1.0 + "query" : "this is a test" } } }"""; @@ -153,7 +150,7 @@ public void testFromJson() throws IOException { "query" : "this is a test", "slop" : 2, "zero_terms_query" : "ALL", - "boost" : 1.0 + "boost" : 2.0 } } }"""; diff --git a/server/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java index 4e6d95515ed13..ce74e471b3e74 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java @@ -272,6 +272,25 @@ public void testToQueryBooleanPrefixMultipleFields() throws IOException { } } + public void testSimpleFromJson() throws IOException { + String json = """ + { + "multi_match" : { + "query" : "quick brown fox", + "fields" : [ "title^1.0", "title.original^1.0", "title.shingles^1.0" ] + } + }"""; + + MultiMatchQueryBuilder parsed = (MultiMatchQueryBuilder) parseQuery(json); + checkGeneratedJson(json, parsed); + + assertEquals(json, "quick brown fox", parsed.value()); + assertEquals(json, 3, parsed.fields().size()); + assertEquals(json, Type.BEST_FIELDS, parsed.type()); + assertEquals(json, Operator.OR, parsed.operator()); + assertEquals(json, true, parsed.fuzzyTranspositions()); + } + public void testFromJson() throws IOException { String json = """ { @@ -279,15 +298,15 @@ public void testFromJson() throws IOException { "query" : "quick brown fox", "fields" : [ "title^1.0", "title.original^1.0", "title.shingles^1.0" ], "type" : "most_fields", - "operator" : "OR", - "slop" : 0, - "prefix_length" : 0, - "max_expansions" : 50, - "lenient" : false, - "zero_terms_query" : "NONE", - "auto_generate_synonyms_phrase_query" : true, + "operator" : "AND", + "slop" : 1, + "prefix_length" : 1, + "max_expansions" : 40, + "lenient" : true, + "zero_terms_query" : "ALL", + "auto_generate_synonyms_phrase_query" : false, "fuzzy_transpositions" : false, - "boost" : 1.0 + "boost" : 2.0 } }"""; @@ -297,7 +316,7 @@ public void testFromJson() throws IOException { assertEquals(json, "quick brown fox", parsed.value()); assertEquals(json, 3, parsed.fields().size()); assertEquals(json, MultiMatchQueryBuilder.Type.MOST_FIELDS, parsed.type()); - assertEquals(json, Operator.OR, parsed.operator()); + assertEquals(json, Operator.AND, parsed.operator()); assertEquals(json, false, parsed.fuzzyTranspositions()); } diff --git a/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java index d150007d8e3e5..a5e80b8fd069d 100644 --- a/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java @@ -1078,19 +1078,19 @@ public void testFromJson() throws IOException { "query" : "this AND that OR thus", "default_field" : "content", "fields" : [ ], - "type" : "best_fields", - "tie_breaker" : 0.0, - "default_operator" : "or", - "max_determinized_states" : 10000, - "enable_position_increments" : true, - "fuzziness" : "AUTO", - "fuzzy_prefix_length" : 0, - "fuzzy_max_expansions" : 50, - "phrase_slop" : 0, - "escape" : false, - "auto_generate_synonyms_phrase_query" : true, + "type" : "most_fields", + "tie_breaker" : 0.1, + "default_operator" : "and", + "max_determinized_states" : 8000, + "enable_position_increments" : false, + "fuzziness" : "1", + "fuzzy_prefix_length" : 1, + "fuzzy_max_expansions" : 20, + "phrase_slop" : 1, + "escape" : true, + "auto_generate_synonyms_phrase_query" : false, "fuzzy_transpositions" : false, - "boost" : 1.0 + "boost" : 2.0 } }"""; @@ -1102,6 +1102,24 @@ public void testFromJson() throws IOException { assertEquals(json, false, parsed.fuzzyTranspositions()); } + public void testSimpleFromJson() throws IOException { + String json = """ + { + "query_string" : { + "query" : "this AND that OR thus", + "default_field" : "content", + "fields" : [ ] + } + }"""; + + QueryStringQueryBuilder parsed = (QueryStringQueryBuilder) parseQuery(json); + checkGeneratedJson(json, parsed); + + assertEquals(json, "this AND that OR thus", parsed.queryString()); + assertEquals(json, "content", parsed.defaultField()); + assertEquals(json, true, parsed.fuzzyTranspositions()); + } + public void testExpandedTerms() throws Exception { // Prefix Query query = new QueryStringQueryBuilder("aBc*").field(TEXT_FIELD_NAME) diff --git a/server/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java index 70b10299aa556..f6ac97c8ab38e 100644 --- a/server/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java @@ -340,6 +340,25 @@ public void testNegativeFlags() throws IOException { assertThat(builder, equalTo(otherBuilder)); } + public void testFromSimpleJson() throws IOException { + String json = """ + { + "simple_query_string" : { + "query" : "\\"fried eggs\\" +(eggplant | potato) -frittata", + "fields" : [ "body^5.0" ] + } + }"""; + + SimpleQueryStringBuilder parsed = (SimpleQueryStringBuilder) parseQuery(json); + checkGeneratedJson(json, parsed); + + assertEquals(json, "\"fried eggs\" +(eggplant | potato) -frittata", parsed.value()); + assertEquals(json, 1, parsed.fields().size()); + assertEquals(json, 0, parsed.fuzzyPrefixLength()); + assertEquals(json, 50, parsed.fuzzyMaxExpansions()); + assertEquals(json, true, parsed.fuzzyTranspositions()); + } + public void testFromJson() throws IOException { String json = """ { @@ -347,16 +366,16 @@ public void testFromJson() throws IOException { "query" : "\\"fried eggs\\" +(eggplant | potato) -frittata", "fields" : [ "body^5.0" ], "analyzer" : "snowball", - "flags" : -1, + "flags" : 8, "default_operator" : "and", "lenient" : false, - "analyze_wildcard" : false, + "analyze_wildcard" : true, "quote_field_suffix" : ".quote", - "auto_generate_synonyms_phrase_query" : true, + "auto_generate_synonyms_phrase_query" : false, "fuzzy_prefix_length" : 1, "fuzzy_max_expansions" : 5, "fuzzy_transpositions" : false, - "boost" : 1.0 + "boost" : 2.0 } }""";