# Backward Pagination with Elasticsearch Aggregation with Spring data Elasticsearch

**URL:** <https://discuss.elastic.co/t/backward-pagination-with-elasticsearch-aggregation-with-spring-data-elasticsearch/324607>\
**Category:** Elasticsearch\
**Created:** [February 3, 2023, 7:15am UTC](https://discuss.elastic.co/t/backward-pagination-with-elasticsearch-aggregation-with-spring-data-elasticsearch/324607 "2023-02-03T07:15:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anuja\_Brahmankar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anuja_brahmankar/32/116722_2.png) [@Anuja\_Brahmankar](https://discuss.elastic.co/u/Anuja_Brahmankar)\
**Post date:** [February 3, 2023, 7:15am UTC](https://discuss.elastic.co/t/backward-pagination-with-elasticsearch-aggregation-with-spring-data-elasticsearch/324607/1 "2023-02-03T07:15:43Z")

</div>

Hi ,  
I have aggregation Query with Composite Aggregation builder with spring data elasticsearch which provides after\_key value,which return last aggregated count with That I am able do forward pagination.  
Sample Query:

```auto
"aggs": {
    "my_buckets": {
      "composite": {
        "sources": [
          {
            "price": {
              "terms": {
                "field": "price.discounted"
              }
            }
          }
        ]
      }
    }
  }

```

For backward pagination ,didn't found any proper reference.  
Could anyone help,please?

---

<div class="post-metadata">

**Author:** ![BenB196](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benb196/32/83401_2.png) [@BenB196](https://discuss.elastic.co/u/BenB196)\
**Post date:** [February 5, 2023, 4:36pm UTC](https://discuss.elastic.co/t/backward-pagination-with-elasticsearch-aggregation-with-spring-data-elasticsearch/324607/2 "2023-02-05T16:36:47Z")

</div>

I'm not too familiar with this topic, but have you looked into changing the [sort](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html) of the query? If you invert the sort, I'd expect you'd get a similar result to backward pagination, but using forward pagination.

---

<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:** [March 5, 2023, 4:37pm UTC](https://discuss.elastic.co/t/backward-pagination-with-elasticsearch-aggregation-with-spring-data-elasticsearch/324607/3 "2023-03-05T16:37:16Z")

</div>

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