0

I want to do a filter aggregation, nested aggregation, and then 3 sibling aggregations. I have tested this and it works using the REST API. Here is the query I used:

{
  "aggs": {
    "filter_agg": {
      "filter": {
        ...
      },
      "aggs": {
        "nested_agg": {
          "nested": {
            "path": "myProperty"
          },
          "aggs": {
            "sibling_agg_1": {
              ...
            },
            "sibling_agg_2": {
              ...
            },
            "sibling_agg_3": {
              ...
            }
          }
        }
      }
    }
  }
}

Now, I want to implement this query using the Java APIs in my codebase. I am not sure how to do sibling aggregations, as the only method available to the FilterAggregationBuilder is subAggregation().

In the context of the Java API is are these sibling aggregations sub aggregations?

The Java code I have started to write looks like this:

client.prepareSearch(index)
    .setTypes(types)
    .setSearchType(SearchType.COUNT)
    .addAggregation(myFilterAggregation
      .subAggregation(AggregationBuilders.nested("nested_agg").path("myProperty"))

and then the only thing I can continue to do to build the SearchRequestBuilder is more subAggregation's - is that the correct way to implement this? The REST API and Java API seem different in this case.

1 Answer 1

2

You can do it like this by adding sub-aggregation to the nested one (note that I chose a terms aggregation since it wasn't clear from your query DSL what specific aggregation type you're using as the sibling aggregations):

// create the nested aggregation
NestedBuilder nestedAgg = AggregationBuilders.nested("nested_agg").path("myProperty");

// now add each of your sub-aggregation to it
nestedAgg.subAggregation(AggregationBuilders.terms("sibling_agg_1").field("field1"));
nestedAgg.subAggregation(AggregationBuilders.terms("sibling_agg_2").field("field2"));
nestedAgg.subAggregation(AggregationBuilders.terms("sibling_agg_3").field("field3"));

// finally add the nested aggregation to the filter aggregation
client.prepareSearch(index)
    .setTypes(types)
    .setSearchType(SearchType.COUNT)
    .addAggregation(myFilterAggregation
      .subAggregation(nestedAgg);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, what I realized yesterday is that it depends on what you call subAggregation on to determine if it is a child or sibling aggregation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.