title: Elasticsearch Composite aggregation with pagination
body: <p>I am using this query to fetch aggregated result but because the result matching the query criteria is very large the number of buckets is larger than 10000.</p>
  <p>How should I write/modify this query so that I can paginate the result?
  I have read that bucket aggregation doesn't allow pagination but it can be converted into composite aggregation to support pagination of response.</p>
  <p>Any alternate way to paginate the response would also be helpful.</p>
  <pre><code>{
      &quot;query&quot;: {
          &quot;bool&quot;: {
              &quot;must&quot;: [
                  {
                      &quot;range&quot;: {
                          &quot;timestamp&quot;: {
                              &quot;gte&quot;: &quot;2022-04-26T00:00:00.000Z&quot;,
                              &quot;lte&quot;: &quot;2022-04-26T23:59:59.999Z&quot;
                          }
                      }
                  },
                  {
                      &quot;terms&quot;: {
                          &quot;job.keyword&quot;: [
                              &quot;JOB_1&quot;,
                              &quot;JOB_2&quot;,
                              &quot;JOB_3&quot;
                          ]
                      }
                  },
                  {
                      &quot;bool&quot;: {
                          &quot;should&quot;: [
                              {
                                  &quot;nested&quot;: {
                                      &quot;path&quot;: &quot;tags&quot;,
                                      &quot;query&quot;: {
                                          &quot;bool&quot;: {
                                              &quot;should&quot;: [
                                                  {
                                                      &quot;bool&quot;: {
                                                          &quot;must&quot;: [
                                                              {
                                                                  &quot;term&quot;: {
                                                                      &quot;tags.name.keyword&quot;: &quot;jobType&quot;
                                                                  }
                                                              },
                                                              {
                                                                  &quot;term&quot;: {
                                                                      &quot;tags.value.keyword&quot;: &quot;discrete&quot;
                                                                  }
                                                              }
                                                          ]
                                                      }
                                                  }
                                              ]
                                          }
                                      }
                                  }
                              }
                          ]
                      }
                  }
              ]
          }
      },
      &quot;size&quot;: 0,
      &quot;aggs&quot;: {
          &quot;job&quot;: {
              &quot;terms&quot;: {
                  &quot;field&quot;: &quot;job.keyword&quot;
              },
              &quot;aggs&quot;: {
                  &quot;accountId&quot;: {
                      &quot;terms&quot;: {
                          &quot;field&quot;: &quot;accountId.keyword&quot;,
                          &quot;size&quot;: 10000
                      },
                      &quot;aggs&quot;: {
                          &quot;accountUsageStats&quot;: {
                              &quot;stats&quot;: {
                                  &quot;field&quot;: &quot;count&quot;
                              }
                          },
                          &quot;tags&quot;: {
                              &quot;top_hits&quot;: {
                                  &quot;size&quot;: 1,
                                  &quot;_source&quot;: {
                                      &quot;include&quot;: [
                                          &quot;tags&quot;
                                      ]
                                  }
                              }
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>