Hi guys, I'm trying to sum data from an top_hits result and I need some help.
First, here's my data:
{"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):
...
{
"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:
...
"aggs": {
"clients": {
"terms": {
"field": "clientId"
},
"aggs": {
"latest_dateTime": {
"max": {
"field": "dateTime"
}
}
...
I added this aggregation, since I need this field too:
"phoneIsValid": {
"top_hits": {
"size": 1,
"_source": "phoneIsValid"
}
},
Then I have this snippet, which should sum how many clients have valid phonenumbers:
"count_valid": {
"sum": {
"field": "clients>phoneIsValid"
}
}
I tried the Sum Bucket Aggregation like this:
"count_valid": {
"sum_bucket": {
"buckets_path": "clients>phoneIsValid"
}
}
And got the following error:
"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:
{
"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
Unfortunately this question is closed without an answer.