Reputation: 2028
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.
Upvotes: 1
Views: 1043
Reputation: 217544
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.
Upvotes: 2