# Sum results from top\_hits aggregation

**URL:** <https://discuss.elastic.co/t/sum-results-from-top-hits-aggregation/126782>\
**Category:** Elasticsearch\
**Created:** [April 4, 2018, 4:12pm UTC](https://discuss.elastic.co/t/sum-results-from-top-hits-aggregation/126782 "2018-04-04T16:12:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfred.bez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alfred.bez/32/29573_2.png) [@alfred.bez](https://discuss.elastic.co/u/alfred.bez)\
**Post date:** [April 4, 2018, 4:12pm UTC](https://discuss.elastic.co/t/sum-results-from-top-hits-aggregation/126782/1 "2018-04-04T16:12:03Z")

</div>

Hi guys, I'm trying to sum data from an top\_hits result and I need some help.

First, here's my data:

```auto
{"clientId":1, "gender": "male", "dateTime":"2017-06-06 17:54:38", "phoneIsValid":1},
{"clientId":1, "gender": "male", "dateTime":"2017-06-01 09:12:56", "phoneIsValid":1},
{"clientId":2, "gender": "female", "dateTime":"2017-06-06 17:54:38", "phoneIsValid":0},
{"clientId":3, "gender": "male", "dateTime":"2017-06-06 17:54:38", "phoneIsValid":1}

```

I want something like this (pseudo):

```auto
...
{
  "key": "male",
  "doc_count": 2,
  "clients": {...},
  "count_email": { "value": 2 }
},
{
  "key": "female",
  "doc_count": 1,
  "clients": {...},
  "count_email": { "value": 0 }
},
...

```

It's possible that we have multiple documents per client, so I need to group them an pick that with the latest "dateTime"-field:

```auto
...
"aggs": {
    "clients": {
        "terms": {
            "field": "clientId"
        },
        "aggs": {
            "latest_dateTime": {
                "max": {
                    "field": "dateTime"
                }
            }
...

```

I added this aggregation, since I need this field too:

```auto
"phoneIsValid": {
    "top_hits": {
        "size": 1,
        "_source": "phoneIsValid"
    }
},

```

Then I have this snippet, which should sum how many clients have valid phonenumbers:

```auto
"count_valid": {
    "sum": {
        "field": "clients>phoneIsValid"
    }
}

```

I tried the Sum Bucket Aggregation like this:

```auto
"count_valid": {
    "sum_bucket": {
        "buckets_path": "clients>phoneIsValid"
    }
}

```

And got the following error:

```auto
"caused_by": {
  "type": "aggregation_execution_exception",
  "reason": "buckets_path must reference either a number value or a single value numeric metric aggregation, got: org.elasticsearch.search.aggregations.metrics.tophits.InternalTopHits"
}

```

Here is my full query:

```auto
{
    "query": {
        "bool": {},
    "size": 0,
    "aggs": {
        "genders": {
            "terms": {
                "field": "gender"
            },
            "aggs": {
                "clients": {
                    "terms": {
                        "field": "clientId"
                    },
                    "aggs": {
                        "latest_dateTime": {
                            "max": {
                                "field": "dateTime"
                            }
                        },
                        "phoneIsValid": {
                            "top_hits": {
                                "size": 1,
                                "_source": "phoneIsValid"
                            }
                        }
                    }
                },
                "count_valid": {
                    "sum_bucket": {
                        "buckets_path": "clients>phoneIsValid"
                    }
                }
            }
        }
    }
}

```

I found this question: [How to sum a field from top\_hits results](https://discuss.elastic.co/t/how-to-sum-a-field-from-top-hits-results/81499)  
Unfortunately this question is closed without an answer.

---

<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:** [May 2, 2018, 4:12pm UTC](https://discuss.elastic.co/t/sum-results-from-top-hits-aggregation/126782/2 "2018-05-02T16:12:17Z")

</div>

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