title: Which is the most effective way to get all the results of aggregation
body: <p>I have the following query: </p>
  
  <pre><code>GET my-index-*/my-type/_search
  {
    "size": 0,
    "aggregations": {
      "my_agg": {
        "terms": {
          "script" : "code"
        },
        "aggs": {
          "dates": {
            "date_range": {
              "field": "created_time",
              "ranges": [
                {
                  "from": "2017-12-09T00:00:00.000",
                  "to": "2017-12-09T16:00:00.000"
                },
                {
                  "from": "2017-12-10T00:00:00.000",
                  "to": "2017-12-10T16:00:00.000"
                }
              ]
            }
          },
          "total_count": {
            "sum_bucket": {
            "buckets_path": "dates._count"
            }
          },
          "bucket_filter": {
            "bucket_selector": {
              "buckets_path": {
                "totalCount": "total_count"
              },
              "script": "params.totalCount == 0"
            }
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>The result of this query is a bunch of buckets. What I need is the list of keys of my buckets. The problem is the aggregation result size is 10 by default, after getting those 10, my <code>bucket_filter</code> filters them by total count, and I get only some of those 10. I need to have all the results, which means I need to specify <code>"size" = n</code>, where <code>n</code> is the distinct count of <code>code</code> values, so that I don't lose any data. I have billions of documents, so in my case <code>n</code> is about 30.000. When I tried executing the query, "Out of memory" occurred on cluster, so I guess it's not the best idea. Is there a good way to get all the results for my query?</p>