# Pagination within a terms aggregation

**URL:** <https://discuss.elastic.co/t/pagination-within-a-terms-aggregation/299996>\
**Category:** Elasticsearch\
**Created:** [March 17, 2022, 11:31pm UTC](https://discuss.elastic.co/t/pagination-within-a-terms-aggregation/299996 "2022-03-17T23:31:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![qcoumes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/qcoumes/32/103191_2.png) [@qcoumes](https://discuss.elastic.co/u/qcoumes)\
**Post date:** [March 17, 2022, 11:31pm UTC](https://discuss.elastic.co/t/pagination-within-a-terms-aggregation/299996/1 "2022-03-17T23:31:50Z")

</div>

I have a nested field `datas` which is inside the also nested field `documents`.

I'd like to retrieve the paginated list of `concepts` buckets (built using a `terms` aggregation).

I managed to retrieve the list without any problem, but I can't manage to paginate this list using `size` and `from` within a `bucket_sort` :

```auto
{
  'aggs': {
    'all_datas': {
      'aggs': {
        'concepts': {
          'aggs': {
            'pagination': {
              'bucket_sort': {
                'from': <offset>,
                'size': <limit>
              }
            }
          },
          'terms': {
            'field': 'documents.datas.thesaurus.concept_code.keyword',
            'show_term_doc_count_error': true
          }
        }
      },
      'nested': {
        'path': 'documents.datas'
      }
    }
  }
}

```

Any idea why it is not working ?

---

<div class="post-metadata">

**Author:** ![qcoumes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/qcoumes/32/103191_2.png) [@qcoumes](https://discuss.elastic.co/u/qcoumes)\
**Post date:** [March 18, 2022, 7:27am UTC](https://discuss.elastic.co/t/pagination-within-a-terms-aggregation/299996/2 "2022-03-18T07:27:53Z")

</div>

It seems that `terms` aggregation does not support `bucket_sort`. I solved my problem using partitions:

I first fetched the total number of concept using cardinality, and I then used the following json:

```json
{
  'aggs': {
    'all_datas': {
      'aggs': {
        'concepts': {
          'terms': {
            'field': 'documents.datas.thesaurus.concept_code.keyword',
            'show_term_doc_count_error': true,
            'size': <limit>,
            'include': {
               "partition": <floor(offset / limit)>,
               "num_partitions": <ceil(cardinality/limit)>
            }
          }
        }
      },
      'nested': {
        'path': 'documents.datas'
      }
    }
  }
}

```

The only real constraint is that offset must be a multiple of limit to not obtain duplicate result between "pages".

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 15, 2022, 7:28am UTC](https://discuss.elastic.co/t/pagination-within-a-terms-aggregation/299996/3 "2022-04-15T07:28:38Z")

</div>

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