# Get surrounding documents vía API

**URL:** https://discuss.elastic.co/t/get-surrounding-documents-via-api/224622
**Category:** Kibana
**Created:** [March 23, 2020, 8:27am UTC](https://discuss.elastic.co/t/get-surrounding-documents-via-api/224622 "2020-03-23T08:27:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![elasticUser5](https://avatars.discourse-cdn.com/v4/letter/e/cc9497/32.png) [@elasticUser5](https://discuss.elastic.co/u/elasticUser5)
#### Post date: [March 23, 2020, 8:27am UTC](https://discuss.elastic.co/t/get-surrounding-documents-via-api/224622/1 "2020-03-23T08:27:25Z")

</div>

Hello!

Is there any way to replicate "Surrounding Documents" function from Kibana but via API?

I'm trying different ways, but none is working (span\_near, scroll API...).

Any help is appreciated!

---

<div class="post-metadata">

### Author: ![markov00](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/markov00/32/33316_2.png) [@markov00](https://discuss.elastic.co/u/markov00)
#### Post date: [March 23, 2020, 12:23pm UTC](https://discuss.elastic.co/t/get-surrounding-documents-via-api/224622/2 "2020-03-23T12:23:58Z")

</div>

Hi @elasticUser5  
If you take a look at that Kibana do when requesting the surrounding documents, you can find that we are doing 3 searches:

1. get the specific document
2. get a max of 5 documents that are 1 day older then the date of the retrieved document at (1)
3. get a max o 5 documents that are 1 day after then the date of the retrieved document at (1)

remember to sort the documents request by the timestamp to get them in the correct order and use the [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-search-after](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-search-after)

---

<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 20, 2020, 12:24pm UTC](https://discuss.elastic.co/t/get-surrounding-documents-via-api/224622/3 "2020-04-20T12:24:14Z")

</div>

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