Skip to content

Commit

Permalink
Shrink slow log for match query
Browse files Browse the repository at this point in the history
This removes defaults from the output of the `match` query's
`toXContent`. That'll make it take up less room in the slow log. And
anywhere else is shows up.

Relates to elastic#76515
  • Loading branch information
nik9000 committed Jan 31, 2022
1 parent 035fdd8 commit 326a568
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,27 +328,41 @@ 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);
}
if (fuzzyRewrite != null) {
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 326a568

Please sign in to comment.