Elasticsearch aggregation over full index but over query results is needed

I have what I think should be a simple query. I'm trying to return the documents in descending order by amount and ascending by name (actually, it's name.keyword). For the cases with duplicate names, I only want one name (so it's an aggregation). The problem is that Elasticsearch is ignoring the query results when applying the aggregation. It's as if the aggregation is over the entire data and not the query results. Here's my query:

    {
        "from": 0,
        "size": 5,
        "sort": [
            {
                "amount": "desc",
                "name.keyword": "asc"
            }
        ],
        "query": {
            "bool": {
                "must": [
                    {
                        "exists": {
                            "field": "name"
                        }
                    },
                    {
                        "exists": {
                            "field": "amount"
                        }
                    }
                ]
            }
        },
        "aggs": {
            "buckets": {
                "composite": {
                    "size": 5,
                    "sources": [
                        {
                            "name": {
                                "terms": {
                                    "field": "name.keyword",
                                    "order": "asc"
                                }
                            }
                        }
                    ]
                }
            }
        }
    }

Any idea how to do the aggregation over the query requests only and not the full index?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.