My data objects contain 3 string fields: section, category and username. I want to do grouping by category and username and find out the top 20 "section" and their count in the grouping i did
basically the result i want is like the sql query: select section, count(*) from table group by category, username order by 2 desc limit 20
i tried doing aggregation on section, category and username. The result was the grouping i was looking for, but it didn't sort the "section" field by this grouping (e.g i need the first result to contain the section that appears in the most combinations of category and username and its count)
instead, it sorted the section just by its doc_count and inside of each section, it sorted "category" by its doc_count and inside of it, it sorted each "username" by its doc_count
This is what I tried:
GET some_index/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{
"match": {
"some_filter_i_needed": "filter_value"
}
}
]
}
},
"aggs": {
"top_section": {
"terms": {
"field": "section.keyword",
"size": 20,
"order": {
"_count": "desc"
}
},
"aggs": {
"top_category": {
"terms": {
"field": "category.keyword",
"size": 1,
"order": {
"_count": "desc"
}
},
"aggs": {
"top_username": {
"terms": {
"field": "username.keyword",
"size": 1 ,
"order": {
"_count": "desc"
}
}
}
}
}
}
}
}
}
Is there some way to get the output i'm looking for?
Thanks

