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

Share XContent rendering code in terms aggs #23680

Merged
merged 1 commit into from
Mar 22, 2017
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 @@ -86,18 +86,11 @@ Bucket newBucket(long docCount, InternalAggregations aggs, long docCountError) {
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
protected final XContentBuilder keyToXContent(XContentBuilder builder) throws IOException {
builder.field(CommonFields.KEY, term);
if (format != DocValueFormat.RAW) {
builder.field(CommonFields.KEY_AS_STRING, format.format(term));
}
builder.field(CommonFields.DOC_COUNT, getDocCount());
if (showDocCountError) {
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, getDocCountError());
}
aggregations.toXContentInternal(builder, params);
builder.endObject();
return builder;
}

Expand Down Expand Up @@ -149,18 +142,6 @@ protected DoubleTerms create(String name, List<Bucket> buckets, long docCountErr
shardSize, showTermDocCountError, otherDocCount, buckets, docCountError);
}

@Override
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, docCountError);
builder.field(SUM_OF_OTHER_DOC_COUNTS, otherDocCount);
builder.startArray(CommonFields.BUCKETS);
for (Bucket bucket : buckets) {
bucket.toXContent(builder, params);
}
builder.endArray();
return builder;
}

@Override
protected Bucket[] createBucketsArray(int size) {
return new Bucket[size];
Expand All @@ -171,7 +152,7 @@ public InternalAggregation doReduce(List<InternalAggregation> aggregations, Redu
boolean promoteToDouble = false;
for (InternalAggregation agg : aggregations) {
if (agg instanceof LongTerms && ((LongTerms) agg).format == DocValueFormat.RAW) {
/**
/*
* this terms agg mixes longs and doubles, we must promote longs to doubles to make the internal aggs
* compatible
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ protected void writeTermTo(StreamOutput out) throws IOException {
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
protected final XContentBuilder keyToXContent(XContentBuilder builder) throws IOException {
throw new UnsupportedOperationException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;

Expand Down Expand Up @@ -127,4 +128,9 @@ protected boolean doEquals(Object obj) {
protected int doHashCode() {
return Objects.hash(super.doHashCode(), buckets, format, otherDocCount, showTermDocCountError, shardSize);
}

@Override
public final XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
return doXContentCommon(builder, params, docCountError, otherDocCount, buckets);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.AggregationExecutionException;
import org.elasticsearch.search.aggregations.Aggregations;
Expand Down Expand Up @@ -141,6 +142,21 @@ public B reduce(List<B> buckets, ReduceContext context) {
return newBucket(docCount, aggs, docCountError);
}

@Override
public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
keyToXContent(builder);
builder.field(CommonFields.DOC_COUNT, getDocCount());
if (showDocCountError) {
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, getDocCountError());
}
aggregations.toXContentInternal(builder, params);
builder.endObject();
return builder;
}

protected abstract XContentBuilder keyToXContent(XContentBuilder builder) throws IOException;

@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) {
Expand Down Expand Up @@ -319,4 +335,16 @@ protected boolean doEquals(Object obj) {
protected int doHashCode() {
return Objects.hash(minDocCount, order, requiredSize);
}

protected static XContentBuilder doXContentCommon(XContentBuilder builder, Params params,
long docCountError, long otherDocCount, List<? extends Bucket> buckets) throws IOException {
builder.field(DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, docCountError);
builder.field(SUM_OF_OTHER_DOC_COUNTS, otherDocCount);
builder.startArray(CommonFields.BUCKETS);
for (Bucket bucket : buckets) {
bucket.toXContent(builder, params);
}
builder.endArray();
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,11 @@ Bucket newBucket(long docCount, InternalAggregations aggs, long docCountError) {
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
protected final XContentBuilder keyToXContent(XContentBuilder builder) throws IOException {
builder.field(CommonFields.KEY, term);
if (format != DocValueFormat.RAW) {
builder.field(CommonFields.KEY_AS_STRING, format.format(term));
}
builder.field(CommonFields.DOC_COUNT, getDocCount());
if (showDocCountError) {
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, getDocCountError());
}
aggregations.toXContentInternal(builder, params);
builder.endObject();
return builder;
}

Expand Down Expand Up @@ -149,18 +142,6 @@ protected LongTerms create(String name, List<Bucket> buckets, long docCountError
showTermDocCountError, otherDocCount, buckets, docCountError);
}

@Override
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, docCountError);
builder.field(SUM_OF_OTHER_DOC_COUNTS, otherDocCount);
builder.startArray(CommonFields.BUCKETS);
for (Bucket bucket : buckets) {
bucket.toXContent(builder, params);
}
builder.endArray();
return builder;
}

@Override
protected Bucket[] createBucketsArray(int size) {
return new Bucket[size];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,8 @@ Bucket newBucket(long docCount, InternalAggregations aggs, long docCountError) {
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(CommonFields.KEY, getKeyAsString());
builder.field(CommonFields.DOC_COUNT, getDocCount());
if (showDocCountError) {
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, getDocCountError());
}
aggregations.toXContentInternal(builder, params);
builder.endObject();
return builder;
protected final XContentBuilder keyToXContent(XContentBuilder builder) throws IOException {
return builder.field(CommonFields.KEY, getKeyAsString());
}

@Override
Expand Down Expand Up @@ -145,18 +137,6 @@ protected StringTerms create(String name, List<Bucket> buckets, long docCountErr
showTermDocCountError, otherDocCount, buckets, docCountError);
}

@Override
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, docCountError);
builder.field(SUM_OF_OTHER_DOC_COUNTS, otherDocCount);
builder.startArray(CommonFields.BUCKETS);
for (Bucket bucket : buckets) {
bucket.toXContent(builder, params);
}
builder.endArray();
return builder;
}

@Override
protected Bucket[] createBucketsArray(int size) {
return new Bucket[size];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -102,11 +103,8 @@ public InternalAggregation doReduce(List<InternalAggregation> aggregations, Redu
}

@Override
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
builder.field(InternalTerms.DOC_COUNT_ERROR_UPPER_BOUND_FIELD_NAME, 0);
builder.field(SUM_OF_OTHER_DOC_COUNTS, 0);
builder.startArray(CommonFields.BUCKETS).endArray();
return builder;
public final XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
return doXContentCommon(builder, params, 0, 0, Collections.emptyList());
}

@Override
Expand Down