Reputation: 15
how to filter the aggs data in elasticsearch
i aggs the docs with my condition and successfully get the result.but i don't know how to filter the result doc count
this is my DSL
{
"size": 0,
"query": {
"bool": {
"filter": [
{
"term": {
"company": "ailsx"
}
}
]
}
},
"aggs": {
"date_aggs": {
"terms": {
"field": "date",
"size": 30
},
"aggs": {
"wang": {
"terms": {
"field": "customer"
}
}
}
}
}
}
this is my result
{
"took": 1864,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 378426,
"max_score": 0,
"hits": [
]
},
"aggregations": {
"date_aggs": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1553212800000,
"key_as_string": "2019-03-22 00:00:00",
"doc_count": 107879,
"wang": {
"doc_count_error_upper_bound": 10,
"sum_other_doc_count": 107855,
"buckets": [
{
"key": "sfa",
"doc_count": 4
},
{
"key": "237692469",
"doc_count": 3
},
{
"key": "fasf",
"doc_count": 3
},
{
"key": "1111daaa",
"doc_count": 2
},
{
"key": "2011",
"doc_count": 2
}]}}]}}
i want to filter the doc_count which bigger than 2, and sum the count,like this
SELECT Count() FROM doc GROUP BY date, customer HAVING count()>=2
Upvotes: 0
Views: 580
Reputation: 217504
You can use min_doc_count
for this purpose:
"wang": {
"terms": {
"field": "customer",
"min_doc_count": 2 <-- add this
}
}
Then the doc_count
property of the bucket will be your sum.
Upvotes: 1