Skip to content

Commit acf9968

Browse files
authored
Shrink join queries in slow log (#83914)
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
1 parent c84c7d4 commit acf9968

File tree

4 files changed

+73
-10
lines changed

4 files changed

+73
-10
lines changed

modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public class HasParentQueryBuilder extends AbstractQueryBuilder<HasParentQueryBu
4848
*/
4949
public static final boolean DEFAULT_IGNORE_UNMAPPED = false;
5050

51+
private static final boolean DEFAULT_SCORE = false;
52+
5153
private static final ParseField QUERY_FIELD = new ParseField("query");
5254
private static final ParseField PARENT_TYPE_FIELD = new ParseField("parent_type");
5355
private static final ParseField SCORE_FIELD = new ParseField("score");
@@ -198,9 +200,13 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
198200
builder.field(QUERY_FIELD.getPreferredName());
199201
query.toXContent(builder, params);
200202
builder.field(PARENT_TYPE_FIELD.getPreferredName(), parentType);
201-
builder.field(SCORE_FIELD.getPreferredName(), score);
202-
builder.field(IGNORE_UNMAPPED_FIELD.getPreferredName(), ignoreUnmapped);
203-
printBoostAndQueryName(builder);
203+
if (score != DEFAULT_SCORE) {
204+
builder.field(SCORE_FIELD.getPreferredName(), score);
205+
}
206+
if (ignoreUnmapped != DEFAULT_IGNORE_UNMAPPED) {
207+
builder.field(IGNORE_UNMAPPED_FIELD.getPreferredName(), ignoreUnmapped);
208+
}
209+
boostAndQueryNameToXContent(builder);
204210
if (innerHitBuilder != null) {
205211
builder.field(INNER_HITS_FIELD.getPreferredName(), innerHitBuilder, params);
206212
}
@@ -210,7 +216,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
210216
public static HasParentQueryBuilder fromXContent(XContentParser parser) throws IOException {
211217
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
212218
String parentType = null;
213-
boolean score = false;
219+
boolean score = DEFAULT_SCORE;
214220
String queryName = null;
215221
InnerHitBuilder innerHits = null;
216222
boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;

modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public final class ParentIdQueryBuilder extends AbstractQueryBuilder<ParentIdQue
4747
private final String type;
4848
private final String id;
4949

50-
private boolean ignoreUnmapped = false;
50+
private boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
5151

5252
public ParentIdQueryBuilder(String type, String id) {
5353
this.type = type;
@@ -103,8 +103,10 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
103103
builder.startObject(NAME);
104104
builder.field(TYPE_FIELD.getPreferredName(), type);
105105
builder.field(ID_FIELD.getPreferredName(), id);
106-
builder.field(IGNORE_UNMAPPED_FIELD.getPreferredName(), ignoreUnmapped);
107-
printBoostAndQueryName(builder);
106+
if (ignoreUnmapped != DEFAULT_IGNORE_UNMAPPED) {
107+
builder.field(IGNORE_UNMAPPED_FIELD.getPreferredName(), ignoreUnmapped);
108+
}
109+
boostAndQueryNameToXContent(builder);
108110
builder.endObject();
109111
}
110112

modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,50 @@ public void testFromJson() throws IOException {
200200
},
201201
"parent_type" : "blog",
202202
"score" : true,
203-
"ignore_unmapped" : false,
204-
"boost" : 1.0
203+
"ignore_unmapped" : true,
204+
"boost" : 2.0
205205
}
206206
}""";
207207
HasParentQueryBuilder parsed = (HasParentQueryBuilder) parseQuery(json);
208208
checkGeneratedJson(json, parsed);
209209
assertEquals(json, "blog", parsed.type());
210210
assertEquals(json, "something", ((TermQueryBuilder) parsed.query()).value());
211+
assertEquals(json, true, parsed.ignoreUnmapped());
212+
}
213+
214+
public void testParseDefaultsRemoved() throws IOException {
215+
String json = """
216+
{
217+
"has_parent" : {
218+
"query" : {
219+
"term" : {
220+
"tag" : {
221+
"value" : "something",
222+
"boost" : 1.0
223+
}
224+
}
225+
},
226+
"parent_type" : "blog",
227+
"score" : false,
228+
"ignore_unmapped" : false,
229+
"boost" : 1.0
230+
}
231+
}""";
232+
checkGeneratedJson("""
233+
{
234+
"has_parent" : {
235+
"query" : {
236+
"term" : {
237+
"tag" : {
238+
"value" : "something",
239+
"boost" : 1.0
240+
}
241+
}
242+
},
243+
"parent_type" : "blog"
244+
}
245+
}""", parseQuery(json));
246+
211247
}
212248

213249
public void testIgnoreUnmapped() throws IOException {

modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void testFromJson() throws IOException {
113113
"parent_id" : {
114114
"type" : "child",
115115
"id" : "123",
116-
"ignore_unmapped" : false,
116+
"ignore_unmapped" : true,
117117
"boost" : 3.0,
118118
"_name" : "name" }
119119
}""";
@@ -125,6 +125,25 @@ public void testFromJson() throws IOException {
125125
assertThat(queryBuilder.queryName(), Matchers.equalTo("name"));
126126
}
127127

128+
public void testDefaultsRemoved() throws IOException {
129+
String query = """
130+
{
131+
"parent_id" : {
132+
"type" : "child",
133+
"id" : "123",
134+
"ignore_unmapped" : false,
135+
"boost" : 1.0
136+
}
137+
}""";
138+
checkGeneratedJson("""
139+
{
140+
"parent_id" : {
141+
"type" : "child",
142+
"id" : "123"
143+
}
144+
}""", parseQuery(query));
145+
}
146+
128147
public void testIgnoreUnmapped() throws IOException {
129148
final ParentIdQueryBuilder queryBuilder = new ParentIdQueryBuilder("unmapped", "foo");
130149
queryBuilder.ignoreUnmapped(true);

0 commit comments

Comments
 (0)