HI,
I am stuck with a use case where I need to group N number of persons by company and get the total count of grouped persons across the pages. So far the query I have prepared is working fine up to the level where the grouping is performed but I need two things:
- List 50 records per page and use the pagination for more records
- Get the total count of persons grouped by company across the pages to set the total number of records for pagination on the front end.
{
"size": 0,
"aggs": {
"group_by_company": {
"composite": {
"size": 50,
"sources": [
{
"company_name": {
"terms": {
"field": "company_name.keyword"
}
}
}
]
},
"aggs": {
"persons": {
"top_hits": {
"size": 1,
"_source": ["person_id", "company_id", "company_name"]
}
},
"persons_count": {
"terms": {
"field": "person_id",
"size": 1
}
}
}
},
"total_persons_count": {
"sum_bucket": {
"buckets_path": "group_by_company>persons_count"
}
}
}
}
This is the response of a single bucket from the above query:
{
"key": "",
"doc_count": 698478,
"persons": {
"hits": {
"total": {
"value": 698478,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "persons",
"_id": "5392465",
"_score": 1,
"_source": {
"person_id": 5392465,
"company_name": "",
"company_id": "0"
}
}
]
}
},
"persons_count": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 698477,
"buckets": [
{
"key": 3,
"doc_count": 1
}
]
}
}
So in this bucket, we have the persons_count which counts the top hits, now what I want is, to sum up the count of hits array or the persons_count bucket across all the pages. I am trying to sum up the persons_count bucket but it gives an error due to the composite. Please see the following error:
{
"error": {
"root_cause": [
{
"type": "action_request_validation_exception",
"reason": "Validation Failed: 1: Unable to find unqualified multi-bucket aggregation in buckets_path. Path must include a multi-bucket aggregation for aggregation [total_persons_count] found :org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder for buckets path: group_by_company>persons_count;"
}
],
"type": "action_request_validation_exception",
"reason": "Validation Failed: 1: Unable to find unqualified multi-bucket aggregation in buckets_path. Path must include a multi-bucket aggregation for aggregation [total_persons_count] found :org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder for buckets path: group_by_company>persons_count;"
},
"status": 400
}
Any help would be appreciated. Please let me know if you need any other information.