Expand search result from a document id

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.

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

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.

1 Like

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