Bool Search with should and count for each should hits

Hi,

I have a question. Is there a possibility to perform a bool search with should and have also the number of hits per should clause in perhaps a aggregate function.

I would need this to build up a statistic.
A possible solution would be to perform the search individual (single) for each term and aggregate this later in the program. But I would like to have this in one query because we have many should aspects in the query.

Is there a better way rather using single queries?

Thanks for your help
Ralf

You can use a filter aggregation per should clause for this:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html

For example:

"query": {
    "bool" : {
        "should" : [
            {
                "term" : { "tag" : "wow" }
            },
            {
                "term" : { "tag" : "elasticsearch" }
            }
        ]
    },
},
"aggs" : {
    "wow" : {
        "filter" : { "term": { "tag": "wow" } }
    },
    "elasticsearch" : {
        "filter" : { "term": { "tag": "elasticsearch" } }
    }
}

If you don't care about document results (i.e. size = 0) or the overall total and don't use any kind of fancy minimum_should_match or other things in the query part, you can also use "match_all" for the query part.