Aggregation for Individual Query in should Array

Hi All,

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

{
    "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:

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

Any suggestion is Much Appreciated.
Thanks.

Hi Manishekar,
Check out the filters aggregation to group sub aggregations by queries.

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

{
    "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"
                    }
                }
            }
        }
    }
}

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.

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