How to use the Bucket Selector to emulate min_doc_count?

I want to eliminate all empty buckets from a range aggregation.

According to https://github.com/elastic/elasticsearch/issues/10027 the Bucket Selector Pipeline Aggregation is the way to go, but I'm struggling with its syntax. Here's what I have so far:

{
  "aggs": {
    "values_histogram": {
      "range": {
        "field": "value",
        "ranges": [
          { "from": 1, "to": 2 },
          { "from": 2, "to": 3 },
          { "from": 3, "to": 4 }
        ]
      },
      "aggs": {
        "the_filter": {
          "bucket_selector": {
            "buckets_path": {
              "the_doc_count": "values_histogram.doc_count"
            },
            "script": "the_doc_count > 0"
          }
        }
      }
    }
  }
}

But this gives me the error:

No aggregation found for path [values_histogram.doc_count]

If you change your buckets_path to the following it should work:

"buckets_path": {
    "the_doc_count": "_count"
},

The buckets_path is relative to the parent aggregation (in this case values_histogram) and _count and _key are both special keywords to indicate the document count of the bucket and the key of the bucket respectively.

Hope that helps

3 Likes