How to get elasticsearch minimum buckets count


(SK) #1

I want to get results on an aggregation where buckets.length (number / count of buckets) has a minimum count, kind of like HAVING in SQL.

(In this particular scenario site visitors who have visited at-least 5 different days.)

A portion of my query:

 "aggs": {
"site_visitors": {
    "aggs": {
        "users": {
            "terms": {
                "field": "user_id",
                "size": 0
            },
            "aggs": {
                "daily_counts": {
                    "date_histogram": {
                        "field": "timestamp",
                        "interval": "1d"
                    }
                }
            }
        }
    }
}

}
Here we have a bucket with 5 results (buckets.length). Most buckets won't have 5 or more results and I don't want all of those.

 {
 "key": 123456,
 "doc_count": 10,
 "days_count": {
    "buckets": [
         {
            "key_as_string": "2017-04-05T00:00:00.000Z",
            "key": 1491350400000,
            "doc_count": 2
         },
         {
            "key_as_string": "2017-04-22T00:00:00.000Z",
            "key": 1492819200000,
            "doc_count": 1
         },
         {
            "key_as_string": "2017-04-25T00:00:00.000Z",
            "key": 1493078400000,
            "doc_count": 4
         },
         {
            "key_as_string": "2017-04-30T00:00:00.000Z",
            "key": 1493510400000,
            "doc_count": 2
         },
         {
            "key_as_string": "2017-05-04T00:00:00.000Z",
            "key": 1493856000000,
            "doc_count": 1
         }
    ]
 }

}

I want to get results that ONLY have at-least 5 results or more (eg: gte) in each bucket - not the doc_count, but the buckets.length.

Is there a way to do this?


(system) #2

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