# Aggregation for Individual Query in should Array

**URL:** <https://discuss.elastic.co/t/aggregation-for-individual-query-in-should-array/283251>\
**Category:** Elasticsearch\
**Created:** [September 3, 2021, 7:13am UTC](https://discuss.elastic.co/t/aggregation-for-individual-query-in-should-array/283251 "2021-09-03T07:13:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manisekhar\_techo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manisekhar_techo/32/78185_2.png) [@Manisekhar\_techo](https://discuss.elastic.co/u/Manisekhar_techo)\
**Post date:** [September 3, 2021, 7:13am UTC](https://discuss.elastic.co/t/aggregation-for-individual-query-in-should-array/283251/1 "2021-09-03T07:13:01Z")

</div>

Hi All,

is there a way for me to perform the aggregation for all the Matching documents inside the bool should array individually.

```auto
{
    "query": {
        "bool": {
            "should": [
                {
                    "multi_match": {
                        "fields": [
                            "title",
                            "abstract"
                        ],
                        "query": "welding",
                        "type": "phrase",
                        "_name": "welding"
                    }
                },
                {
                    "multi_match": {
                        "fields": [
                            "title",
                            "abstract"
                        ],
                        "query": "gmaw",
                        "type": "phrase",
                        "_name": "gmaw"
                    }
                }
            ]
        }
    },
    "aggregations": {
        "dates": {
            "date_histogram": {
                "field": "date",
                "interval": "year",
                "format": "yyyy"
            }
        }
    }
}

```

this will give me Doc count per Year for the Above query.  
But, can I get the Aggregations separately for each query item in the should array.  
I'm expecting something like this:

```auto
{
    "welding": [
        {
            "year": 2020,
            "count": 15
        },
        {
            "year": 2019,
            "count": 11
        }
    ],
    "gmaw": [
         {
            "year": 2020,
            "count": 15
        },
        {
            "year": 2019,
            "count": 11
        }
    ]
}

```

Any suggestion is Much Appreciated.  
Thanks.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 3, 2021, 12:50pm UTC](https://discuss.elastic.co/t/aggregation-for-individual-query-in-should-array/283251/2 "2021-09-03T12:50:47Z")

</div>

Hi Manishekar,  
Check out the [filters aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/7.14/search-aggregations-bucket-filters-aggregation.html) to group sub aggregations by queries.

---

<div class="post-metadata">

**Author:** ![Manisekhar\_techo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/manisekhar_techo/32/78185_2.png) [@Manisekhar\_techo](https://discuss.elastic.co/u/Manisekhar_techo)\
**Post date:** [September 3, 2021, 1:41pm UTC](https://discuss.elastic.co/t/aggregation-for-individual-query-in-should-array/283251/3 "2021-09-03T13:41:14Z")

</div>

Thanks Mark for the Help.  
I've used the same filters aggregation and nested the "aggs" for the desired date histogram aggregation.

```auto
{
    "size": 0,
    "aggs": {
        "gmaw": {
            "filter": {
                "multi_match": {
                    "query": "gmaw",
                    "type": "phrase",
                    "fields": [
                        "title"
                    ],
                    "boost": 25,
                    "_name": "gmaw"
                }
            },
            "aggs": {
                "dates": {
                    "date_histogram": {
                        "field": "date",
                        "interval": "year",
                        "format": "yyyy"
                    }
                }
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 3, 2021, 2:05pm UTC](https://discuss.elastic.co/t/aggregation-for-individual-query-in-should-array/283251/4 "2021-09-03T14:05:40Z")

</div>

That's the `filter` aggregation.  
The `filters` aggregation (note the plural) lets you list multiple queries e.g. like the 2 in your original question.

---

<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:** [October 1, 2021, 2:05pm UTC](https://discuss.elastic.co/t/aggregation-for-individual-query-in-should-array/283251/5 "2021-10-01T14:05:53Z")

</div>

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