# Expand search result from a document id

**URL:** https://discuss.elastic.co/t/expand-search-result-from-a-document-id/238616
**Category:** Elasticsearch
**Created:** [June 25, 2020, 7:51am UTC](https://discuss.elastic.co/t/expand-search-result-from-a-document-id/238616 "2020-06-25T07:51:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Thomas\_Ardal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_ardal/32/7956_2.png) [@Thomas\_Ardal](https://discuss.elastic.co/u/Thomas_Ardal)
#### Post date: [June 25, 2020, 7:51am UTC](https://discuss.elastic.co/t/expand-search-result-from-a-document-id/238616/1 "2020-06-25T07:51:14Z")

</div>

I have an index of time-based data. I want to be able to expand a search result for a document with an id. Something like "show the 10 documents before and the 10 documents after document with id x". I have a couple of ideas but all of them require me to fetch more documents than needed and then throwing away documents in code. Is there a built-in feature that will help be create a query like this?

As information, I'm using Elasticsearch 6.4. If a feature to solve this exists only in a newer version, I still want to know about it since upgrading is a possibility.

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [June 25, 2020, 8:23am UTC](https://discuss.elastic.co/t/expand-search-result-from-a-document-id/238616/2 "2020-06-25T08:23:26Z")

</div>

Kibana does this, but I don't know how it works under the hood sorry ☹

---

<div class="post-metadata">

### Author: ![Thomas\_Ardal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thomas_ardal/32/7956_2.png) [@Thomas\_Ardal](https://discuss.elastic.co/u/Thomas_Ardal)
#### Post date: [June 25, 2020, 8:48am UTC](https://discuss.elastic.co/t/expand-search-result-from-a-document-id/238616/3 "2020-06-25T08:48:48Z")

</div>

Oh, you're right. There's a surrounding documents search. I never noticed that before but it does exactly what I'm looking for here. Thank you so much!

For reference, Kibana implements this by first making a search for a document by id. Then getting the timestamp from the single result. Then create a new multi-search containing two queries. One for documents with timestamp lower than that of the single doc. And another for documents with timestamp higher than that of the single doc. Both ordered by timestamp and a limit of returned documents of course.

---

<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: [July 23, 2020, 9:01am UTC](https://discuss.elastic.co/t/expand-search-result-from-a-document-id/238616/5 "2020-07-23T09:01:57Z")

</div>

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