# Does k-nearest neighbor (kNN) search - supports search\_after pagination?

**URL:** <https://discuss.elastic.co/t/does-k-nearest-neighbor-knn-search-supports-search-after-pagination/315222>\
**Category:** Elasticsearch\
**Tags:** vector-search\
**Created:** [September 27, 2022, 12:13am UTC](https://discuss.elastic.co/t/does-k-nearest-neighbor-knn-search-supports-search-after-pagination/315222 "2022-09-27T00:13:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dinesh\_Sonachalam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dinesh_sonachalam/32/111191_2.png) [@Dinesh\_Sonachalam](https://discuss.elastic.co/u/Dinesh_Sonachalam)\
**Post date:** [September 27, 2022, 12:13am UTC](https://discuss.elastic.co/t/does-k-nearest-neighbor-knn-search-supports-search-after-pagination/315222/1 "2022-09-27T00:13:23Z")

</div>

**Reference:**

1. kNN search: [k-nearest neighbor (kNN) search | Elasticsearch Guide [master] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/master/knn-search.html#approximate-knn)
2. Search after pagination: [https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-aft](https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after)

---

<div class="post-metadata">

**Author:** ![Dazhi\_Jiao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dazhi_jiao/32/111760_2.png) [@Dazhi\_Jiao](https://discuss.elastic.co/u/Dazhi_Jiao)\
**Post date:** [October 6, 2022, 3:21pm UTC](https://discuss.elastic.co/t/does-k-nearest-neighbor-knn-search-supports-search-after-pagination/315222/2 "2022-10-06T15:21:40Z")

</div>

I have a same question. What is the suggested pagination approach if using KNN?

---

<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 3, 2022, 3:22pm UTC](https://discuss.elastic.co/t/does-k-nearest-neighbor-knn-search-supports-search-after-pagination/315222/3 "2022-11-03T15:22:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Julie\_Tibshirani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/julie_tibshirani/32/55628_2.png) [@Julie\_Tibshirani](https://discuss.elastic.co/u/Julie_Tibshirani)\
**Post date:** [November 7, 2022, 11:44pm UTC](https://discuss.elastic.co/t/does-k-nearest-neighbor-knn-search-supports-search-after-pagination/315222/4 "2022-11-07T23:44:47Z")

</div>

Sorry for the slow response! Yes, search\_after is supported with approximate kNN search. You can think of the 'knn' section as adding 'k' matching documents to the search. If you set k=100 and size=20, then you will only show the first 20 matching documents. Then you could use search\_after (or another paging technique like from/ size) to page through the rest of the matching documents.

However, with approximate kNN search it is not possible to page through **all** documents in order of their vector similarity. For example, you cannot say "return only the nearest 'k' documents, now fetch the next nearest 'k' documents, etc." until you page through all the documents. Approximate kNN search algorithms do not allow this sort of paging.

If you need to do that, you either need to set a large 'k' up-front like 'k=100', so you can page through those 'k' documents. Or, you could use exact kNN ([k-nearest neighbor (kNN) search | Elasticsearch Guide [8.5] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.5/knn-search.html#exact-knn)) if you really need to be able to page through the whole set of documents in order of vector similarity.
