1

I am doing aggregation like this:

  {
   "size":0,
   "aggs":
    {
      "my_aggs":
      {
         "terms":
          {
            "field":"my_field"
          }
      }
   }
}

I want to get only the aggregation result. So when I set size=0 like below, it gives error-later learned this is for how many results I want(aggregations). So, how can I achieve this to get only the aggregation results(no hits result docs)

AbstractAggregationBuilder aggregationBuilder = AggregationBuilders
                .terms("my_aggs")
                .field("my_field")
                .size(0) //how to set size for my purpose?
                .order(BucketOrder.key(true));

Moreover, If I get thousands of aggregation results, does this query return all of them? or apply to its default 10 size? If not, how do know how many should I set size of aggregation result.


Edit I am adding my aggregation like this:

 SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withIndices(indexName)
                .withTypes(typeName)
                .withQuery(boolQueryBuilder)
                .addAggregation(aggregationBuilder)
                .build();

Please help.

1 Answer 1

2

You do that on the SearchRequest instance not at the aggregation level:

AbstractAggregationBuilder aggregationBuilder = AggregationBuilders
            .terms("my_aggs")
            .field("my_field")
            .order(BucketOrder.key(true));

SearchQuery searchQuery = new NativeSearchQueryBuilder()
            .withIndices(indexName)
            .withTypes(typeName)
            .withQuery(boolQueryBuilder)
            .withPageable(new PageRequest(0, 1))       <--- add this
            .addAggregation(aggregationBuilder)
            .build();

As far as I know, Spring Data ES doesn't allow you to create a PageRequest with size 0 (hence why I picked 1). If that's a problem, this answer shows you how to override that behavior.

By default, your aggregation will return 10 buckets, but you can increase the size up to 10000, if needed.

Sign up to request clarification or add additional context in comments.

2 Comments

according to the link, we can use " .withMaxResults(0)" github.com/spring-projects/spring-data-elasticsearch/issues/…
Same results with ".setPageable(Pageable.ofSize(1));"

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.