Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shrink join queries in slow log #83914

Merged
merged 1 commit into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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