Correctly applying criteria for sum aggregation

Hello,

I am dealing with a use case where, given an elastic index with information of a number of people, I am querying for those that have the first name of "John" and get the sum of their ages. The query in question looks like:

GET sample_index/_search
{
    "aggregations":{
        "sum_age":{
            "sum":{
                "field": "age"
            }
        }
    },
    "query":{
        "bool":{
            "filter":[{
                "bool":{
                    "should":[{
                        "multi_match":{
                            "fields":["name^1.0","alias^1.0"],
                            "query":"John",
                            "type":"best_fields"
                        }
                    }]
                }
            }]
        }   
    },
    "size":50,
    "timeout":"60s",
    "track_total_hits": -1
}

The example above represents an incorrect version of what I am trying to achieve.

I'd like to be able to search by the word "John" across the name and alias text fields, get 50 results at most, then get the sum of the values of the age field in the returned results. However, the actual outcome is something different: the result would limit the displayed results to 50 records, but the aggregation is done across the entire result set,which may contain well over 50 records. I know, unless I am mistaken, sum aggregation does not support size. What kind of changes would I need to make it so that I can aggregate based on the query result, or the same metrics?

Also, this is my first post in this forum, please let me know if any action needs to be taken on this topic, and I will act accordingly.

Thank you.

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