-
-
Save clement-tourriere/3571d3dc22a1cd33534f to your computer and use it in GitHub Desktop.
Elasticsearch Binary subaggregations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If I change toXContent from AggregationBuilder.java, it produces a correct output | |
@Override | |
public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | |
builder.startObject(getName()); | |
builder.field(type); | |
internalXContent(builder, params); | |
if (aggregations != null || aggregationsBinary != null) { | |
if (aggregations != null) { | |
builder.startObject("aggregations"); | |
for (AbstractAggregationBuilder subAgg : aggregations) { | |
subAgg.toXContent(builder, params); | |
} | |
builder.endObject(); | |
} | |
else { | |
if (XContentFactory.xContentType(aggregationsBinary) == builder.contentType()) { | |
builder.rawField("aggregations", aggregationsBinary); | |
} else { | |
builder.field("aggregations_binary", aggregationsBinary); | |
} | |
} | |
} | |
return builder.endObject(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.elasticsearch.common.xcontent.ToXContent; | |
import org.elasticsearch.common.xcontent.XContentBuilder; | |
import org.elasticsearch.common.xcontent.XContentHelper; | |
import org.elasticsearch.common.xcontent.json.JsonXContent; | |
import org.elasticsearch.search.aggregations.AggregationBuilders; | |
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; | |
import java.io.IOException; | |
public class TestSubAggregation { | |
public static void main(String[] args) throws IOException { | |
// Build a simple term aggregation | |
TermsBuilder termsBuilder = AggregationBuilders.terms("test").field("testfield"); | |
// Build a simple term sub aggregation | |
TermsBuilder subTerm = AggregationBuilders.terms("subtest").field("subtestfield"); | |
// Add sub aggregation as an AggregationBuilder | |
termsBuilder.subAggregation(subTerm); | |
// It produces a correct output | |
System.out.println(XContentHelper.toString(termsBuilder)); | |
// Reset term aggregation | |
termsBuilder = AggregationBuilders.terms("test").field("testfield"); | |
// Create an XContentBuilder from sub aggregation | |
XContentBuilder subTermContentBuilder = JsonXContent.contentBuilder().startObject(); | |
subTerm.toXContent(subTermContentBuilder, ToXContent.EMPTY_PARAMS); | |
subTermContentBuilder.endObject(); | |
// Add sub aggregation as a XContentBuilder (binary_aggregation) | |
termsBuilder.subAggregation(subTermContentBuilder); | |
// Produces an incorrect output (two aggregations levels instead of one) | |
System.out.println(XContentHelper.toString(termsBuilder)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment