title: Elasticsearch. Using term aggregation, return values where doc count is less than some value
body: <p>I want to group values by field(account id in my case)  using term aggregation and return only fields where doc_count is less than some value.</p>
  
  <p>I can specify min_doc_count parameter, but there is no max_doc_count. So I'm looking for a way to simulate this behavior. One of my many tries is this, but it doesn't work.</p>
  
  <pre><code>{
    "size": 0,
    "aggs": {
      "by_account": {
        "terms": {
          "field": "accountId"
        },
        "aggs": {
          "by_account_filtered": {
            "bucket_selector": {
              "buckets_path": {
                "totalDocs": "_count"
              },
              "script": "params.totalDocs &lt; 10000"
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>What am I doing wrong?</p>