# How to check if ES has more documents when paginating with search after API?

**URL:** <https://discuss.elastic.co/t/how-to-check-if-es-has-more-documents-when-paginating-with-search-after-api/151419>\
**Category:** Elasticsearch\
**Created:** [October 8, 2018, 9:31am UTC](https://discuss.elastic.co/t/how-to-check-if-es-has-more-documents-when-paginating-with-search-after-api/151419 "2018-10-08T09:31:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vroad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vroad/32/36242_2.png) [@vroad](https://discuss.elastic.co/u/vroad)\
**Post date:** [October 8, 2018, 9:31am UTC](https://discuss.elastic.co/t/how-to-check-if-es-has-more-documents-when-paginating-with-search-after-api/151419/1 "2018-10-08T09:31:58Z")

</div>

I'm trying to implement pagination of my GraphQL API with ElasticSearch's pagination feature. My pagination implementation in GraphQL API follows Relay specification. As far as I know, search after feature in ES only returns cursors in Relay specification, which can be used to tell ES which item you fetched in the last request.  
In Relay specification, client could also check if there are more items in next page with `hasNextPage` property.  
But how can I implement hasNextPage API without this information from ES?

Composite aggregation has similar pagination feature as search after API, and does not have properties like `hasNextPage` either.

Only method I can think of is including integer id in cursor. Pagination initially starts without cursor, so assign 1 to the first item. When the integer id becomes the same number as the total document document, there are no items left. This may not work properly if documents get added or removed in ES, though.

[https://facebook.github.io/relay/graphql/connections.htm](https://facebook.github.io/relay/graphql/connections.htm)

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-after.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-after.html)  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html#\_after](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html#_after)

---

<div class="post-metadata">

**Author:** ![Murilo\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@Murilo\_Tavares](https://discuss.elastic.co/u/Murilo_Tavares)\
**Post date:** [October 25, 2018, 6:41pm UTC](https://discuss.elastic.co/t/how-to-check-if-es-has-more-documents-when-paginating-with-search-after-api/151419/2 "2018-10-25T18:41:20Z")

</div>

I am in the same situation here.  
For now the workaround I have in mind is to always query for page+1 elements, and returning only the first page'th elements to the user. If the query returns less than page+1 elements we know there's no next page. In other words, let's say the user asks for 10 elements. We ask ES for 11 elements. If ES returns 11 elements, then we return the first 10 to the user and set hasNextPage as true.

But this solution does not make me proud at all, and also has a small extra cost associated, so I wonder if there's any nicer way of doing this using ElasticSearch.

---

<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:** [November 22, 2018, 6:41pm UTC](https://discuss.elastic.co/t/how-to-check-if-es-has-more-documents-when-paginating-with-search-after-api/151419/3 "2018-11-22T18:41:21Z")

</div>

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