diff --git a/server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java index fb45597fb4771..f1a31db6125d2 100644 --- a/server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java @@ -78,13 +78,30 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws protected abstract void doXContent(XContentBuilder builder, Params params) throws IOException; - protected void printBoostAndQueryName(XContentBuilder builder) throws IOException { + /** + * Add {@code boost} and {@code query_name} to the builder. + * @deprecated use {@link #boostAndQueryNameToXContent} + */ + @Deprecated + protected final void printBoostAndQueryName(XContentBuilder builder) throws IOException { builder.field(BOOST_FIELD.getPreferredName(), boost); if (queryName != null) { builder.field(NAME_FIELD.getPreferredName(), queryName); } } + /** + * Add {@code boost} and {@code query_name} to the builder. + */ + protected final void boostAndQueryNameToXContent(XContentBuilder builder) throws IOException { + if (boost != DEFAULT_BOOST) { + builder.field(BOOST_FIELD.getPreferredName(), boost); + } + if (queryName != null) { + builder.field(NAME_FIELD.getPreferredName(), queryName); + } + } + @Override public final Query toQuery(SearchExecutionContext context) throws IOException { Query query = doToQuery(context); diff --git a/server/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java index 36ff50db646b5..171638825ed20 100644 --- a/server/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/MatchQueryBuilder.java @@ -328,15 +328,21 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio builder.startObject(fieldName); builder.field(QUERY_FIELD.getPreferredName(), value); - builder.field(OPERATOR_FIELD.getPreferredName(), operator.toString()); + if (operator != Operator.OR) { + builder.field(OPERATOR_FIELD.getPreferredName(), operator.toString()); + } if (analyzer != null) { builder.field(ANALYZER_FIELD.getPreferredName(), analyzer); } if (fuzziness != null) { fuzziness.toXContent(builder, params); } - builder.field(PREFIX_LENGTH_FIELD.getPreferredName(), prefixLength); - builder.field(MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions); + if (prefixLength != FuzzyQuery.defaultPrefixLength) { + builder.field(PREFIX_LENGTH_FIELD.getPreferredName(), prefixLength); + } + if (maxExpansions != FuzzyQuery.defaultMaxExpansions) { + builder.field(MAX_EXPANSIONS_FIELD.getPreferredName(), maxExpansions); + } if (minimumShouldMatch != null) { builder.field(MINIMUM_SHOULD_MATCH_FIELD.getPreferredName(), minimumShouldMatch); } @@ -344,11 +350,19 @@ public void doXContent(XContentBuilder builder, Params params) throws IOExceptio builder.field(FUZZY_REWRITE_FIELD.getPreferredName(), fuzzyRewrite); } // LUCENE 4 UPGRADE we need to document this & test this - builder.field(FUZZY_TRANSPOSITIONS_FIELD.getPreferredName(), fuzzyTranspositions); - builder.field(LENIENT_FIELD.getPreferredName(), lenient); - builder.field(ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString()); - builder.field(GENERATE_SYNONYMS_PHRASE_QUERY.getPreferredName(), autoGenerateSynonymsPhraseQuery); - printBoostAndQueryName(builder); + if (fuzzyTranspositions != FuzzyQuery.defaultTranspositions) { + builder.field(FUZZY_TRANSPOSITIONS_FIELD.getPreferredName(), fuzzyTranspositions); + } + if (lenient != MatchQueryParser.DEFAULT_LENIENCY) { + builder.field(LENIENT_FIELD.getPreferredName(), lenient); + } + if (false == zeroTermsQuery.equals(MatchQueryParser.DEFAULT_ZERO_TERMS_QUERY)) { + builder.field(ZERO_TERMS_QUERY_FIELD.getPreferredName(), zeroTermsQuery.toString()); + } + if (autoGenerateSynonymsPhraseQuery == false) { + builder.field(GENERATE_SYNONYMS_PHRASE_QUERY.getPreferredName(), autoGenerateSynonymsPhraseQuery); + } + boostAndQueryNameToXContent(builder); builder.endObject(); builder.endObject(); } diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java index b1819af573a4f..f000c64807ea7 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java @@ -258,12 +258,33 @@ public void testSimpleMatchQuery() throws IOException { } }"""; MatchQueryBuilder qb = (MatchQueryBuilder) parseQuery(json); - checkGeneratedJson(json, qb); + checkGeneratedJson(""" + { + "match": { + "message": { + "query": "to be or not to be", + "operator": "AND", + "zero_terms_query": "ALL" + } + } + }""", qb); assertEquals(json, "to be or not to be", qb.value()); assertEquals(json, Operator.AND, qb.operator()); } + public void testToXConentWithDefaults() throws IOException { + QueryBuilder query = new MatchQueryBuilder("foo", "bar"); + checkGeneratedJson(""" + { + "match": { + "foo": { + "query": "bar" + } + } + }""", query); + } + public void testFuzzinessOnNonStringField() throws Exception { MatchQueryBuilder query = new MatchQueryBuilder(INT_FIELD_NAME, 42); query.fuzziness(randomFuzziness(INT_FIELD_NAME)); diff --git a/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java index a9f121e9dc349..0dfd7a03fb260 100644 --- a/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java @@ -178,7 +178,34 @@ public void testFromJson() throws IOException { }"""; NestedQueryBuilder parsed = (NestedQueryBuilder) parseQuery(json); - checkGeneratedJson(json, parsed); + checkGeneratedJson(""" + { + "nested" : { + "query" : { + "bool" : { + "must" : [ { + "match" : { + "obj1.name" : { + "query" : "blue" + } + } + }, { + "range" : { + "obj1.count" : { + "gt" : 5, + "boost" : 1.0 + } + } + } ], + "boost" : 1.0 + } + }, + "path" : "obj1", + "ignore_unmapped" : false, + "score_mode" : "avg", + "boost" : 1.0 + } + }""", parsed); assertEquals(json, ScoreMode.Avg, parsed.scoreMode()); }