Skip to content

Commit 9617f42

Browse files
committed
Share XContent rendering code in terms aggs (#23680)
The output of the different implementations of terms aggs is always very similar. The toXContent methods for each of those classes though was duplicating almost the same code multiple times. This commit centralizes the code for rendering XContent to a single place, which can be reused from the different terms aggs implementations.
1 parent c7f5c98 commit 9617f42

File tree

7 files changed

+42
-68
lines changed

7 files changed

+42
-68
lines changed

core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/DoubleTerms.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,11 @@ Bucket newBucket(long docCount, InternalAggregations aggs, long docCountError) {
8383
}
8484

8585
@Override
86-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
87-
builder.startObject();
86+
protected final XContentBuilder keyToXContent(XContentBuilder builder) throws IOException {
8887
builder.field(CommonFields.KEY, term);
8988
if (format != DocValueFormat.RAW) {
9089
builder.field(CommonFields.KEY_AS_STRING, format.format(term));
9190
}
92-
builder.field(CommonFields.DOC_COUNT, getDocCount());
93-
if (showDocCountError) {
94-
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, getDocCountError());
95-
}
96-
aggregations.toXContentInternal(builder, params);
97-
builder.endObject();
9891
return builder;
9992
}
10093
}
@@ -136,18 +129,6 @@ protected DoubleTerms create(String name, List<Bucket> buckets, long docCountErr
136129
shardSize, showTermDocCountError, otherDocCount, buckets, docCountError);
137130
}
138131

139-
@Override
140-
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
141-
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, docCountError);
142-
builder.field(SUM_OF_OTHER_DOC_COUNTS, otherDocCount);
143-
builder.startArray(CommonFields.BUCKETS);
144-
for (Bucket bucket : buckets) {
145-
bucket.toXContent(builder, params);
146-
}
147-
builder.endArray();
148-
return builder;
149-
}
150-
151132
@Override
152133
protected Bucket[] createBucketsArray(int size) {
153134
return new Bucket[size];

core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/GlobalOrdinalsStringTermsAggregator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ protected void writeTermTo(StreamOutput out) throws IOException {
246246
}
247247

248248
@Override
249-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
249+
protected final XContentBuilder keyToXContent(XContentBuilder builder) throws IOException {
250250
throw new UnsupportedOperationException();
251251
}
252252
}

core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalMappedTerms.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.elasticsearch.common.io.stream.StreamInput;
2323
import org.elasticsearch.common.io.stream.StreamOutput;
24+
import org.elasticsearch.common.xcontent.XContentBuilder;
2425
import org.elasticsearch.search.DocValueFormat;
2526
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
2627

@@ -110,4 +111,9 @@ public B getBucketByKey(String term) {
110111
}
111112
return bucketMap.get(term);
112113
}
114+
115+
@Override
116+
public final XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
117+
return doXContentCommon(builder, params, docCountError, otherDocCount, buckets);
118+
}
113119
}

core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/InternalTerms.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.elasticsearch.common.io.stream.StreamInput;
2222
import org.elasticsearch.common.io.stream.StreamOutput;
2323
import org.elasticsearch.common.xcontent.ToXContent;
24+
import org.elasticsearch.common.xcontent.XContentBuilder;
2425
import org.elasticsearch.search.DocValueFormat;
2526
import org.elasticsearch.search.aggregations.AggregationExecutionException;
2627
import org.elasticsearch.search.aggregations.Aggregations;
@@ -139,6 +140,21 @@ public B reduce(List<B> buckets, ReduceContext context) {
139140
InternalAggregations aggs = InternalAggregations.reduce(aggregationsList, context);
140141
return newBucket(docCount, aggs, docCountError);
141142
}
143+
144+
@Override
145+
public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
146+
builder.startObject();
147+
keyToXContent(builder);
148+
builder.field(CommonFields.DOC_COUNT, getDocCount());
149+
if (showDocCountError) {
150+
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, getDocCountError());
151+
}
152+
aggregations.toXContentInternal(builder, params);
153+
builder.endObject();
154+
return builder;
155+
}
156+
157+
protected abstract XContentBuilder keyToXContent(XContentBuilder builder) throws IOException;
142158
}
143159

144160
protected final Terms.Order order;
@@ -286,4 +302,16 @@ public InternalAggregation doReduce(List<InternalAggregation> aggregations, Redu
286302
* Create an array to hold some buckets. Used in collecting the results.
287303
*/
288304
protected abstract B[] createBucketsArray(int size);
305+
306+
protected static XContentBuilder doXContentCommon(XContentBuilder builder, Params params,
307+
long docCountError, long otherDocCount, List<? extends Bucket> buckets) throws IOException {
308+
builder.field(DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, docCountError);
309+
builder.field(SUM_OF_OTHER_DOC_COUNTS, otherDocCount);
310+
builder.startArray(CommonFields.BUCKETS);
311+
for (Bucket bucket : buckets) {
312+
bucket.toXContent(builder, params);
313+
}
314+
builder.endArray();
315+
return builder;
316+
}
289317
}

core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/LongTerms.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,11 @@ Bucket newBucket(long docCount, InternalAggregations aggs, long docCountError) {
8383
}
8484

8585
@Override
86-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
87-
builder.startObject();
86+
protected final XContentBuilder keyToXContent(XContentBuilder builder) throws IOException {
8887
builder.field(CommonFields.KEY, term);
8988
if (format != DocValueFormat.RAW) {
9089
builder.field(CommonFields.KEY_AS_STRING, format.format(term));
9190
}
92-
builder.field(CommonFields.DOC_COUNT, getDocCount());
93-
if (showDocCountError) {
94-
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, getDocCountError());
95-
}
96-
aggregations.toXContentInternal(builder, params);
97-
builder.endObject();
9891
return builder;
9992
}
10093
}
@@ -136,18 +129,6 @@ protected LongTerms create(String name, List<Bucket> buckets, long docCountError
136129
showTermDocCountError, otherDocCount, buckets, docCountError);
137130
}
138131

139-
@Override
140-
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
141-
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, docCountError);
142-
builder.field(SUM_OF_OTHER_DOC_COUNTS, otherDocCount);
143-
builder.startArray(CommonFields.BUCKETS);
144-
for (Bucket bucket : buckets) {
145-
bucket.toXContent(builder, params);
146-
}
147-
builder.endArray();
148-
return builder;
149-
}
150-
151132
@Override
152133
protected Bucket[] createBucketsArray(int size) {
153134
return new Bucket[size];

core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/StringTerms.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,8 @@ Bucket newBucket(long docCount, InternalAggregations aggs, long docCountError) {
8484
}
8585

8686
@Override
87-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
88-
builder.startObject();
89-
builder.field(CommonFields.KEY, getKeyAsString());
90-
builder.field(CommonFields.DOC_COUNT, getDocCount());
91-
if (showDocCountError) {
92-
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, getDocCountError());
93-
}
94-
aggregations.toXContentInternal(builder, params);
95-
builder.endObject();
96-
return builder;
87+
protected final XContentBuilder keyToXContent(XContentBuilder builder) throws IOException {
88+
return builder.field(CommonFields.KEY, getKeyAsString());
9789
}
9890
}
9991

@@ -134,18 +126,6 @@ protected StringTerms create(String name, List<Bucket> buckets, long docCountErr
134126
showTermDocCountError, otherDocCount, buckets, docCountError);
135127
}
136128

137-
@Override
138-
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
139-
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, docCountError);
140-
builder.field(SUM_OF_OTHER_DOC_COUNTS, otherDocCount);
141-
builder.startArray(CommonFields.BUCKETS);
142-
for (Bucket bucket : buckets) {
143-
bucket.toXContent(builder, params);
144-
}
145-
builder.endArray();
146-
return builder;
147-
}
148-
149129
@Override
150130
protected Bucket[] createBucketsArray(int size) {
151131
return new Bucket[size];

core/src/main/java/org/elasticsearch/search/aggregations/bucket/terms/UnmappedTerms.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
2828

2929
import java.io.IOException;
30+
import java.util.Collections;
3031
import java.util.List;
3132
import java.util.Map;
3233

@@ -102,11 +103,8 @@ public InternalAggregation doReduce(List<InternalAggregation> aggregations, Redu
102103
}
103104

104105
@Override
105-
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
106-
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, 0);
107-
builder.field(SUM_OF_OTHER_DOC_COUNTS, 0);
108-
builder.startArray(CommonFields.BUCKETS).endArray();
109-
return builder;
106+
public final XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
107+
return doXContentCommon(builder, params, 0, 0, Collections.emptyList());
110108
}
111109

112110
@Override

0 commit comments

Comments
 (0)