# Elasticsearch: find closest/nearest docs in reference to one (calculated from known date)

**URL:** https://discuss.elastic.co/t/elasticsearch-find-closest-nearest-docs-in-reference-to-one-calculated-from-known-date/314648
**Category:** Elasticsearch
**Created:** [September 18, 2022, 4:52pm UTC](https://discuss.elastic.co/t/elasticsearch-find-closest-nearest-docs-in-reference-to-one-calculated-from-known-date/314648 "2022-09-18T16:52:14Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![err](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/err/32/77872_2.png) [@err](https://discuss.elastic.co/u/err)
#### Post date: [September 18, 2022, 4:52pm UTC](https://discuss.elastic.co/t/elasticsearch-find-closest-nearest-docs-in-reference-to-one-calculated-from-known-date/314648/1 "2022-09-18T16:52:14Z")

</div>

Is there a standard seach possibility to find closest docs to one reference doc by date criteria (if I don not know range yet)

Example dataset

```auto
index “person”
 - doc : name=>Alice birthday=>01.01.1991
 - doc : name=>Bobby birthday=>01.01.1963
 - doc : name=>Karen birthday=>01.01.2006
 - doc : name=>James birthday=>01.01.1998
 - doc : name=>Chris birthday=>01.01.2000
 - doc : name=>Diana birthday=>01.01.2022
 - doc : name=>Wendy birthday=>01.01.1992

```

**Find closest to James** (2 precedent and 2 following from James' birthdate).

Result would be

```auto
 1. Alice (91) -2
 2. Wendy (93) -1
 3. James (98) 0
 4. Chris (00) 1
 5. Karen (06) 2 

```

Or a hint/example/direction how would you solve it.

---

<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: [October 16, 2022, 4:52pm UTC](https://discuss.elastic.co/t/elasticsearch-find-closest-nearest-docs-in-reference-to-one-calculated-from-known-date/314648/2 "2022-10-16T16:52:29Z")

</div>

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