# Bool Search with should and count for each should hits

**URL:** <https://discuss.elastic.co/t/bool-search-with-should-and-count-for-each-should-hits/57657>\
**Category:** Elasticsearch\
**Created:** [August 10, 2016, 6:31am UTC](https://discuss.elastic.co/t/bool-search-with-should-and-count-for-each-should-hits/57657 "2016-08-10T06:31:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mir\_S](https://avatars.discourse-cdn.com/v4/letter/m/278dde/32.png) [@Mir\_S](https://discuss.elastic.co/u/Mir_S)\
**Post date:** [August 10, 2016, 6:31am UTC](https://discuss.elastic.co/t/bool-search-with-should-and-count-for-each-should-hits/57657/1 "2016-08-10T06:31:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ywelsch](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ywelsch/32/7751_2.png) [@ywelsch](https://discuss.elastic.co/u/ywelsch)\
**Post date:** [August 10, 2016, 7:28am UTC](https://discuss.elastic.co/t/bool-search-with-should-and-count-for-each-should-hits/57657/2 "2016-08-10T07:28:16Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html)

For example:

```auto
"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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:28pm UTC](https://discuss.elastic.co/t/bool-search-with-should-and-count-for-each-should-hits/57657/3 "2017-07-05T22:28:53Z")

</div>


