# Finding nearest timestamps

**URL:** https://discuss.elastic.co/t/finding-nearest-timestamps/205171
**Category:** Elasticsearch
**Created:** [October 25, 2019, 3:24am UTC](https://discuss.elastic.co/t/finding-nearest-timestamps/205171 "2019-10-25T03:24:38Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![joaociocca](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joaociocca/32/15827_2.png) [@joaociocca](https://discuss.elastic.co/u/joaociocca)
#### Post date: [October 25, 2019, 3:24am UTC](https://discuss.elastic.co/t/finding-nearest-timestamps/205171/1 "2019-10-25T03:24:38Z")

</div>

Is there a way to find a nearest timestamp? Let's say I'm looking for the nearest registry (both before and after) 2019-10-09T19:02:22.000Z

At first I was tackling this problem like I had to find this exact timestamp...

For context, I'm searching VPN logs to find out who was connected (login/logout logs) at one point in time. Except it's a lot of different IPs and different times, but I got to dig through how DSL worked and even managed to make a huge should/must nested DSL to nail them all... when I realized that what I needed was something else.

For sharing sake 😉

```auto
{
  "bool": {
  "should": [
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } },
 { "bool": { "must": [{ "range": { "@timestamp": { "gte": "<start_event_date>T<start_event_time>Z", "lt": "<end_event_date>T<end_event_time>Z" } }}, { "match": { "FramedIPAddress": { "query": "<ip>" } } }] } }
  ]
  }
}

```

---

<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 22, 2019, 3:24am UTC](https://discuss.elastic.co/t/finding-nearest-timestamps/205171/2 "2019-11-22T03:24:38Z")

</div>

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