Skip to content

Commit

Permalink
Shrink join queries in slow log (#83914)
Browse files Browse the repository at this point in the history
This removes the defaults from the slow log for the remaining queries in
the `parent-join` module. So it should be easier to read the slow log
when it contains these queries.

Relates to #76515
  • Loading branch information
nik9000 authored Feb 17, 2022
1 parent c84c7d4 commit acf9968
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
*/
public static final boolean DEFAULT_IGNORE_UNMAPPED = false;

private static final boolean DEFAULT_SCORE = false;

private static final ParseField QUERY_FIELD = new ParseField("query");
private static final ParseField PARENT_TYPE_FIELD = new ParseField("parent_type");
private static final ParseField SCORE_FIELD = new ParseField("score");
Expand Down Expand Up @@ -198,9 +200,13 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.field(QUERY_FIELD.getPreferredName());
query.toXContent(builder, params);
builder.field(PARENT_TYPE_FIELD.getPreferredName(), parentType);
builder.field(SCORE_FIELD.getPreferredName(), score);
builder.field(IGNORE_UNMAPPED_FIELD.getPreferredName(), ignoreUnmapped);
printBoostAndQueryName(builder);
if (score != DEFAULT_SCORE) {
builder.field(SCORE_FIELD.getPreferredName(), score);
}
if (ignoreUnmapped != DEFAULT_IGNORE_UNMAPPED) {
builder.field(IGNORE_UNMAPPED_FIELD.getPreferredName(), ignoreUnmapped);
}
boostAndQueryNameToXContent(builder);
if (innerHitBuilder != null) {
builder.field(INNER_HITS_FIELD.getPreferredName(), innerHitBuilder, params);
}
Expand All @@ -210,7 +216,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
public static HasParentQueryBuilder fromXContent(XContentParser parser) throws IOException {
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String parentType = null;
boolean score = false;
boolean score = DEFAULT_SCORE;
String queryName = null;
InnerHitBuilder innerHits = null;
boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class ParentIdQueryBuilder extends AbstractQueryBuilder<ParentIdQue
private final String type;
private final String id;

private boolean ignoreUnmapped = false;
private boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;

public ParentIdQueryBuilder(String type, String id) {
this.type = type;
Expand Down Expand Up @@ -103,8 +103,10 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
builder.startObject(NAME);
builder.field(TYPE_FIELD.getPreferredName(), type);
builder.field(ID_FIELD.getPreferredName(), id);
builder.field(IGNORE_UNMAPPED_FIELD.getPreferredName(), ignoreUnmapped);
printBoostAndQueryName(builder);
if (ignoreUnmapped != DEFAULT_IGNORE_UNMAPPED) {
builder.field(IGNORE_UNMAPPED_FIELD.getPreferredName(), ignoreUnmapped);
}
boostAndQueryNameToXContent(builder);
builder.endObject();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,50 @@ public void testFromJson() throws IOException {
},
"parent_type" : "blog",
"score" : true,
"ignore_unmapped" : false,
"boost" : 1.0
"ignore_unmapped" : true,
"boost" : 2.0
}
}""";
HasParentQueryBuilder parsed = (HasParentQueryBuilder) parseQuery(json);
checkGeneratedJson(json, parsed);
assertEquals(json, "blog", parsed.type());
assertEquals(json, "something", ((TermQueryBuilder) parsed.query()).value());
assertEquals(json, true, parsed.ignoreUnmapped());
}

public void testParseDefaultsRemoved() throws IOException {
String json = """
{
"has_parent" : {
"query" : {
"term" : {
"tag" : {
"value" : "something",
"boost" : 1.0
}
}
},
"parent_type" : "blog",
"score" : false,
"ignore_unmapped" : false,
"boost" : 1.0
}
}""";
checkGeneratedJson("""
{
"has_parent" : {
"query" : {
"term" : {
"tag" : {
"value" : "something",
"boost" : 1.0
}
}
},
"parent_type" : "blog"
}
}""", parseQuery(json));

}

public void testIgnoreUnmapped() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void testFromJson() throws IOException {
"parent_id" : {
"type" : "child",
"id" : "123",
"ignore_unmapped" : false,
"ignore_unmapped" : true,
"boost" : 3.0,
"_name" : "name" }
}""";
Expand All @@ -125,6 +125,25 @@ public void testFromJson() throws IOException {
assertThat(queryBuilder.queryName(), Matchers.equalTo("name"));
}

public void testDefaultsRemoved() throws IOException {
String query = """
{
"parent_id" : {
"type" : "child",
"id" : "123",
"ignore_unmapped" : false,
"boost" : 1.0
}
}""";
checkGeneratedJson("""
{
"parent_id" : {
"type" : "child",
"id" : "123"
}
}""", parseQuery(query));
}

public void testIgnoreUnmapped() throws IOException {
final ParentIdQueryBuilder queryBuilder = new ParentIdQueryBuilder("unmapped", "foo");
queryBuilder.ignoreUnmapped(true);
Expand Down

0 comments on commit acf9968

Please sign in to comment.