Skip to content

Commit

Permalink
Don't output default values in text query xcontent representations (#…
Browse files Browse the repository at this point in the history
…86979)

This removes default values from the xcontent representations of the
following queries:

* intervals
* combined_fields
* match_bool_prefix
* match_phrase_prefix
* match_phrase
* multi_match
* query_string
* simple_query_string
  • Loading branch information
romseygeek authored May 23, 2022
1 parent 2804ff4 commit ca542fe
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ public class CombinedFieldsQueryBuilder extends AbstractQueryBuilder<CombinedFie
private static final ParseField GENERATE_SYNONYMS_PHRASE_QUERY = new ParseField("auto_generate_synonyms_phrase_query");
private static final ParseField ZERO_TERMS_QUERY_FIELD = new ParseField("zero_terms_query");

private static final Operator DEFAULT_OPERATOR = Operator.OR;
private static final ZeroTermsQueryOption DEFAULT_ZERO_TERMS_QUERY = ZeroTermsQueryOption.NONE;
private static final boolean DEFAULT_GENERATE_SYNONYMS_PHRASE = true;

private final Object value;
private final Map<String, Float> 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<CombinedFieldsQueryBuilder, Void> PARSER = new ConstructingObjectParser<>(
NAME,
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,24 +433,38 @@ 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());
}

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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}""";
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}""";
Expand All @@ -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
}
}
}""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}""";
Expand All @@ -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
}
}
}""";
Expand Down
Loading

0 comments on commit ca542fe

Please sign in to comment.