Skip to content

Commit 27af37b

Browse files
authored
MultiBucketsAggregation.Bucket does not implement ToXContent anymore (#117240) (#117306)
This change makes some buckets implementation leaner.
1 parent 950b360 commit 27af37b

File tree

52 files changed

+157
-389
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+157
-389
lines changed

modules/aggregations/src/main/java/org/elasticsearch/aggregations/bucket/adjacency/InternalAdjacencyMatrix.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,12 @@ public InternalAggregations getAggregations() {
8181
return aggregations;
8282
}
8383

84-
@Override
85-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
84+
private void bucketToXContent(XContentBuilder builder, Params params) throws IOException {
8685
builder.startObject();
8786
builder.field(CommonFields.KEY.getPreferredName(), key);
8887
builder.field(CommonFields.DOC_COUNT.getPreferredName(), docCount);
8988
aggregations.toXContentInternal(builder, params);
9089
builder.endObject();
91-
return builder;
9290
}
9391

9492
@Override
@@ -237,7 +235,7 @@ public InternalAggregation finalizeSampling(SamplingContext samplingContext) {
237235
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
238236
builder.startArray(CommonFields.BUCKETS.getPreferredName());
239237
for (InternalBucket bucket : buckets) {
240-
bucket.toXContent(builder, params);
238+
bucket.bucketToXContent(builder, params);
241239
}
242240
builder.endArray();
243241
return builder;

modules/aggregations/src/main/java/org/elasticsearch/aggregations/bucket/histogram/InternalAutoDateHistogram.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ public Object getKey() {
9999
return Instant.ofEpochMilli(key).atZone(ZoneOffset.UTC);
100100
}
101101

102-
@Override
103-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
102+
private void bucketToXContent(XContentBuilder builder, Params params, DocValueFormat format) throws IOException {
104103
String keyAsString = format.format(key).toString();
105104
builder.startObject();
106105
if (format != DocValueFormat.RAW) {
@@ -110,7 +109,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
110109
builder.field(CommonFields.DOC_COUNT.getPreferredName(), docCount);
111110
aggregations.toXContentInternal(builder, params);
112111
builder.endObject();
113-
return builder;
114112
}
115113

116114
@Override
@@ -597,7 +595,7 @@ private BucketReduceResult mergeConsecutiveBuckets(
597595
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
598596
builder.startArray(CommonFields.BUCKETS.getPreferredName());
599597
for (Bucket bucket : buckets) {
600-
bucket.toXContent(builder, params);
598+
bucket.bucketToXContent(builder, params, format);
601599
}
602600
builder.endArray();
603601
builder.field("interval", getInterval().toString());

modules/aggregations/src/main/java/org/elasticsearch/aggregations/bucket/timeseries/InternalTimeSeries.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,21 @@ public class InternalTimeSeries extends InternalMultiBucketAggregation<InternalT
3636
*/
3737
public static class InternalBucket extends InternalMultiBucketAggregation.InternalBucket {
3838
protected long bucketOrd;
39-
protected final boolean keyed;
4039
protected final BytesRef key;
4140
// TODO: make computing docCount optional
4241
protected long docCount;
4342
protected InternalAggregations aggregations;
4443

45-
public InternalBucket(BytesRef key, long docCount, InternalAggregations aggregations, boolean keyed) {
44+
public InternalBucket(BytesRef key, long docCount, InternalAggregations aggregations) {
4645
this.key = key;
4746
this.docCount = docCount;
4847
this.aggregations = aggregations;
49-
this.keyed = keyed;
5048
}
5149

5250
/**
5351
* Read from a stream.
5452
*/
55-
public InternalBucket(StreamInput in, boolean keyed) throws IOException {
56-
this.keyed = keyed;
53+
public InternalBucket(StreamInput in) throws IOException {
5754
key = in.readBytesRef();
5855
docCount = in.readVLong();
5956
aggregations = InternalAggregations.readFrom(in);
@@ -86,8 +83,7 @@ public InternalAggregations getAggregations() {
8683
return aggregations;
8784
}
8885

89-
@Override
90-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
86+
private void bucketToXContent(XContentBuilder builder, Params params, boolean keyed) throws IOException {
9187
// Use map key in the xcontent response:
9288
var key = getKey();
9389
if (keyed) {
@@ -99,7 +95,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
9995
builder.field(CommonFields.DOC_COUNT.getPreferredName(), docCount);
10096
aggregations.toXContentInternal(builder, params);
10197
builder.endObject();
102-
return builder;
10398
}
10499

105100
@Override
@@ -112,14 +107,13 @@ public boolean equals(Object other) {
112107
}
113108
InternalTimeSeries.InternalBucket that = (InternalTimeSeries.InternalBucket) other;
114109
return Objects.equals(key, that.key)
115-
&& Objects.equals(keyed, that.keyed)
116110
&& Objects.equals(docCount, that.docCount)
117111
&& Objects.equals(aggregations, that.aggregations);
118112
}
119113

120114
@Override
121115
public int hashCode() {
122-
return Objects.hash(getClass(), key, keyed, docCount, aggregations);
116+
return Objects.hash(getClass(), key, docCount, aggregations);
123117
}
124118
}
125119

@@ -143,7 +137,7 @@ public InternalTimeSeries(StreamInput in) throws IOException {
143137
int size = in.readVInt();
144138
List<InternalTimeSeries.InternalBucket> buckets = new ArrayList<>(size);
145139
for (int i = 0; i < size; i++) {
146-
buckets.add(new InternalTimeSeries.InternalBucket(in, keyed));
140+
buckets.add(new InternalTimeSeries.InternalBucket(in));
147141
}
148142
this.buckets = buckets;
149143
this.bucketMap = null;
@@ -162,7 +156,7 @@ public XContentBuilder doXContentBody(XContentBuilder builder, Params params) th
162156
builder.startArray(CommonFields.BUCKETS.getPreferredName());
163157
}
164158
for (InternalBucket bucket : buckets) {
165-
bucket.toXContent(builder, params);
159+
bucket.bucketToXContent(builder, params, keyed);
166160
}
167161
if (keyed) {
168162
builder.endObject();
@@ -252,14 +246,14 @@ public InternalTimeSeries create(List<InternalBucket> buckets) {
252246

253247
@Override
254248
public InternalBucket createBucket(InternalAggregations aggregations, InternalBucket prototype) {
255-
return new InternalBucket(prototype.key, prototype.docCount, aggregations, prototype.keyed);
249+
return new InternalBucket(prototype.key, prototype.docCount, aggregations);
256250
}
257251

258252
private InternalBucket reduceBucket(List<InternalBucket> buckets, AggregationReduceContext context) {
259253
InternalTimeSeries.InternalBucket reduced = null;
260254
for (InternalTimeSeries.InternalBucket bucket : buckets) {
261255
if (reduced == null) {
262-
reduced = new InternalTimeSeries.InternalBucket(bucket.key, bucket.docCount, bucket.aggregations, bucket.keyed);
256+
reduced = new InternalTimeSeries.InternalBucket(bucket.key, bucket.docCount, bucket.aggregations);
263257
} else {
264258
reduced.docCount += bucket.docCount;
265259
}

modules/aggregations/src/main/java/org/elasticsearch/aggregations/bucket/timeseries/TimeSeriesAggregator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ public InternalAggregation[] buildAggregations(LongArray owningBucketOrds) throw
8383
InternalTimeSeries.InternalBucket bucket = new InternalTimeSeries.InternalBucket(
8484
BytesRef.deepCopyOf(spare), // Closing bucketOrds will corrupt the bytes ref, so need to make a deep copy here.
8585
docCount,
86-
null,
87-
keyed
86+
null
8887
);
8988
bucket.bucketOrd = ordsEnum.ord();
9089
buckets.add(bucket);

modules/aggregations/src/test/java/org/elasticsearch/aggregations/bucket/timeseries/InternalTimeSeriesTests.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private List<InternalBucket> randomBuckets(boolean keyed, InternalAggregations a
4949
}
5050
try {
5151
var key = TimeSeriesIdFieldMapper.buildLegacyTsid(routingPathFields).toBytesRef();
52-
bucketList.add(new InternalBucket(key, docCount, aggregations, keyed));
52+
bucketList.add(new InternalBucket(key, docCount, aggregations));
5353
} catch (IOException e) {
5454
throw new UncheckedIOException(e);
5555
}
@@ -108,29 +108,29 @@ public void testReduceSimple() {
108108
InternalTimeSeries first = new InternalTimeSeries(
109109
"ts",
110110
List.of(
111-
new InternalBucket(new BytesRef("1"), 3, InternalAggregations.EMPTY, false),
112-
new InternalBucket(new BytesRef("10"), 6, InternalAggregations.EMPTY, false),
113-
new InternalBucket(new BytesRef("2"), 2, InternalAggregations.EMPTY, false),
114-
new InternalBucket(new BytesRef("9"), 5, InternalAggregations.EMPTY, false)
111+
new InternalBucket(new BytesRef("1"), 3, InternalAggregations.EMPTY),
112+
new InternalBucket(new BytesRef("10"), 6, InternalAggregations.EMPTY),
113+
new InternalBucket(new BytesRef("2"), 2, InternalAggregations.EMPTY),
114+
new InternalBucket(new BytesRef("9"), 5, InternalAggregations.EMPTY)
115115
),
116116
false,
117117
Map.of()
118118
);
119119
InternalTimeSeries second = new InternalTimeSeries(
120120
"ts",
121121
List.of(
122-
new InternalBucket(new BytesRef("2"), 1, InternalAggregations.EMPTY, false),
123-
new InternalBucket(new BytesRef("3"), 3, InternalAggregations.EMPTY, false)
122+
new InternalBucket(new BytesRef("2"), 1, InternalAggregations.EMPTY),
123+
new InternalBucket(new BytesRef("3"), 3, InternalAggregations.EMPTY)
124124
),
125125
false,
126126
Map.of()
127127
);
128128
InternalTimeSeries third = new InternalTimeSeries(
129129
"ts",
130130
List.of(
131-
new InternalBucket(new BytesRef("1"), 2, InternalAggregations.EMPTY, false),
132-
new InternalBucket(new BytesRef("3"), 4, InternalAggregations.EMPTY, false),
133-
new InternalBucket(new BytesRef("9"), 4, InternalAggregations.EMPTY, false)
131+
new InternalBucket(new BytesRef("1"), 2, InternalAggregations.EMPTY),
132+
new InternalBucket(new BytesRef("3"), 4, InternalAggregations.EMPTY),
133+
new InternalBucket(new BytesRef("9"), 4, InternalAggregations.EMPTY)
134134
),
135135
false,
136136
Map.of()

modules/aggregations/src/test/java/org/elasticsearch/aggregations/bucket/timeseries/TimeSeriesAggregatorTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,19 @@ public void testMultiBucketAggregationAsSubAggregation() throws IOException {
176176
InternalDateHistogram byTimeStampBucket = ts.getBucketByKey("{dim1=aaa, dim2=xxx}").getAggregations().get("by_timestamp");
177177
assertThat(
178178
byTimeStampBucket.getBuckets(),
179-
contains(new InternalDateHistogram.Bucket(startTime, 2, false, null, InternalAggregations.EMPTY))
179+
contains(new InternalDateHistogram.Bucket(startTime, 2, null, InternalAggregations.EMPTY))
180180
);
181181
assertThat(ts.getBucketByKey("{dim1=aaa, dim2=yyy}").docCount, equalTo(2L));
182182
byTimeStampBucket = ts.getBucketByKey("{dim1=aaa, dim2=yyy}").getAggregations().get("by_timestamp");
183183
assertThat(
184184
byTimeStampBucket.getBuckets(),
185-
contains(new InternalDateHistogram.Bucket(startTime, 2, false, null, InternalAggregations.EMPTY))
185+
contains(new InternalDateHistogram.Bucket(startTime, 2, null, InternalAggregations.EMPTY))
186186
);
187187
assertThat(ts.getBucketByKey("{dim1=bbb, dim2=zzz}").docCount, equalTo(4L));
188188
byTimeStampBucket = ts.getBucketByKey("{dim1=bbb, dim2=zzz}").getAggregations().get("by_timestamp");
189189
assertThat(
190190
byTimeStampBucket.getBuckets(),
191-
contains(new InternalDateHistogram.Bucket(startTime, 4, false, null, InternalAggregations.EMPTY))
191+
contains(new InternalDateHistogram.Bucket(startTime, 4, null, InternalAggregations.EMPTY))
192192
);
193193
};
194194

server/src/main/java/org/elasticsearch/search/aggregations/bucket/MultiBucketsAggregation.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.elasticsearch.search.aggregations.Aggregation;
1313
import org.elasticsearch.search.aggregations.HasAggregations;
1414
import org.elasticsearch.search.aggregations.InternalAggregations;
15-
import org.elasticsearch.xcontent.ToXContent;
1615

1716
import java.util.List;
1817

@@ -24,7 +23,7 @@ public interface MultiBucketsAggregation extends Aggregation {
2423
* A bucket represents a criteria to which all documents that fall in it adhere to. It is also uniquely identified
2524
* by a key, and can potentially hold sub-aggregations computed over all documents in it.
2625
*/
27-
interface Bucket extends HasAggregations, ToXContent {
26+
interface Bucket extends HasAggregations {
2827
/**
2928
* @return The key associated with the bucket
3029
*/

server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/InternalComposite.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,6 @@ public int compareKey(InternalBucket other) {
465465
return 0;
466466
}
467467

468-
@Override
469-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
470-
/**
471-
* See {@link CompositeAggregation#bucketToXContent}
472-
*/
473-
throw new UnsupportedOperationException("not implemented");
474-
}
475-
476468
InternalBucket finalizeSampling(SamplingContext samplingContext) {
477469
return new InternalBucket(
478470
sourceNames,

server/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/FiltersAggregator.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,9 @@ public InternalAggregation[] buildAggregations(LongArray owningBucketOrds) throw
215215
filters.size() + (otherBucketKey == null ? 0 : 1),
216216
(offsetInOwningOrd, docCount, subAggregationResults) -> {
217217
if (offsetInOwningOrd < filters.size()) {
218-
return new InternalFilters.InternalBucket(
219-
filters.get(offsetInOwningOrd).key(),
220-
docCount,
221-
subAggregationResults,
222-
keyed,
223-
keyedBucket
224-
);
218+
return new InternalFilters.InternalBucket(filters.get(offsetInOwningOrd).key(), docCount, subAggregationResults);
225219
}
226-
return new InternalFilters.InternalBucket(otherBucketKey, docCount, subAggregationResults, keyed, keyedBucket);
220+
return new InternalFilters.InternalBucket(otherBucketKey, docCount, subAggregationResults);
227221
},
228222
buckets -> new InternalFilters(name, buckets, keyed, keyedBucket, metadata())
229223
);
@@ -234,12 +228,12 @@ public InternalAggregation buildEmptyAggregation() {
234228
InternalAggregations subAggs = buildEmptySubAggregations();
235229
List<InternalFilters.InternalBucket> buckets = new ArrayList<>(filters.size() + (otherBucketKey == null ? 0 : 1));
236230
for (QueryToFilterAdapter filter : filters) {
237-
InternalFilters.InternalBucket bucket = new InternalFilters.InternalBucket(filter.key(), 0, subAggs, keyed, keyedBucket);
231+
InternalFilters.InternalBucket bucket = new InternalFilters.InternalBucket(filter.key(), 0, subAggs);
238232
buckets.add(bucket);
239233
}
240234

241235
if (otherBucketKey != null) {
242-
InternalFilters.InternalBucket bucket = new InternalFilters.InternalBucket(otherBucketKey, 0, subAggs, keyed, keyedBucket);
236+
InternalFilters.InternalBucket bucket = new InternalFilters.InternalBucket(otherBucketKey, 0, subAggs);
243237
buckets.add(bucket);
244238
}
245239

0 commit comments

Comments
 (0)