# searchAfter cannot work with terminateAfter properly

**URL:** <https://discuss.elastic.co/t/searchafter-cannot-work-with-terminateafter-properly/172946>\
**Category:** Elasticsearch\
**Created:** [March 19, 2019, 10:47am UTC](https://discuss.elastic.co/t/searchafter-cannot-work-with-terminateafter-properly/172946 "2019-03-19T10:47:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Caleb\_Shan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/caleb_shan/32/42288_2.png) [@Caleb\_Shan](https://discuss.elastic.co/u/Caleb_Shan)\
**Post date:** [March 19, 2019, 10:47am UTC](https://discuss.elastic.co/t/searchafter-cannot-work-with-terminateafter-properly/172946/1 "2019-03-19T10:47:06Z")

</div>

elastic version : 6.4.2

Let's say page size is 10, shard count is 1, the matched total hits is 100.  
If we set terminateAfter to 10, we can optain the first page. Then we use searchAfter to get the second page, will get nothing. It seems the docs before searchAfter offset also increased the collect count, so it terminate more earlier than it should be.

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [March 20, 2019, 8:41am UTC](https://discuss.elastic.co/t/searchafter-cannot-work-with-terminateafter-properly/172946/2 "2019-03-20T08:41:40Z")

</div>

Unlike `terminate_after`, `search_after` is only applied to the top documents collector, it doesn't filter documents that should be collected by aggregations for instance. This means that even if top documents are empty because `search_after` filtered them aggregations can have results since they see all documents that match the query no matter how `search_after` is set. If you want to achieve pagination and use `terminate_after` on top of it you'll need to use a query that filters the top documents returned by the previous page. What are your sort criteria ? Are you sorting by a field (multiple fields), is it numeric ? By score only ?

---

<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 17, 2019, 8:41am UTC](https://discuss.elastic.co/t/searchafter-cannot-work-with-terminateafter-properly/172946/3 "2019-04-17T08:41:51Z")

</div>

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