0

I have the following aggregation query that I tested and run fine. I am wondering how to build the equivalent query using its JAVA API. In fact I have a List<String> of each element I'd like to see the stats. How can I do this dynamically?

Thank you in advance.

{
  "from" : 0,
  "size" : 0,
  "aggs": {
    "value_stats" :{
      "stats": {
        "field" : "value"
      }
    },
    "qty_stats":{
      "stats": {"field" : "qty"}
    }
  }
}

1 Answer 1

1

It's pretty straightforward to add aggregations using their AggregationBuilders

// 1. replace this list of fields with yours
List<String> statFields = getFields();

// 2. bootstrap the query
SearchRequestBuilder search = node.client().prepareSearch()
    .setSize(0).setFrom(0)
    .setQuery(QueryBuilders.matchAllQuery());

// 3. add a stats aggregation for each of your fields
for (String field : statFields) {
    search.addAggregation(AggregationBuilders.stats(field+"_stats").field(field));
}

// 4. execute the query
SearchResponse response = search.execute().actionGet();
Sign up to request clarification or add additional context in comments.

1 Comment

I thought I can only call search.addAggregation() once. Thanks !

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.