# Bookending time range

**URL:** <https://discuss.elastic.co/t/bookending-time-range/154533>\
**Category:** Elasticsearch\
**Created:** [October 29, 2018, 11:58pm UTC](https://discuss.elastic.co/t/bookending-time-range/154533 "2018-10-29T23:58:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dstory](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@dstory](https://discuss.elastic.co/u/dstory)\
**Post date:** [October 29, 2018, 11:58pm UTC](https://discuss.elastic.co/t/bookending-time-range/154533/1 "2018-10-29T23:58:34Z")

</div>

Is there a way to get the first result outside of a time range on either side? I'm trying to get a set of historical data within certain time values, but also want the known value when the time range began and ended.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 30, 2018, 4:50am UTC](https://discuss.elastic.co/t/bookending-time-range/154533/2 "2018-10-30T04:50:24Z")

</div>

A min and max aggregation on the date field?

---

<div class="post-metadata">

**Author:** ![dstory](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@dstory](https://discuss.elastic.co/u/dstory)\
**Post date:** [October 30, 2018, 5:07pm UTC](https://discuss.elastic.co/t/bookending-time-range/154533/3 "2018-10-30T17:07:14Z")

</div>

I'm not sure if that's what I'm trying to accomplish. Currently, I can get the exact data I want using three separate queries, which is obviously not ideal:

First query (left bookend):

```
   {
    	"size": 1,
    	"sort": [
    		{
    			"timestamp_value": {
    				"order": "desc"
    			}
    		}
    	],
    	"query": {
    		"bool": {
    			"must": {
    				"match": {
    					"point_id": 5
    				}
    			},
    			"filter": {
    				"range": {
    					"timestamp_value": {
    						"lt": 1540843592000
    					}
    				}
    			}
    		}
    	}
    }

```

Second query:

```
{
	"sort": [
		{
			"timestamp_value": {
				"order": "asc"
			}
		}
	],
	"query": {
		"bool": {
			"must": {
				"match": {
					"point_id": "5"
				}
			},
			"filter": {
				"range": {
					"timestamp_value": {
						"gte": 1540843592000,
						"lte": 1540843791000
					}
				}
			}
		}
	}
}

```

Third query (right bookend):

```
{
	"size": 1,
	"sort": [
		{
			"timestamp_value": {
				"order": "asc"
			}
		}
	],
	"query": {
		"bool": {
			"must": {
				"match": {
					"point_id": 5
				}
			},
			"filter": {
				"range": {
					"timestamp_value": {
						"gt": 1540843791000
					}
				}
			}
		}
	}
}

```

Is there a more efficient way of doing this that I'm missing?

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 30, 2018, 5:32pm UTC](https://discuss.elastic.co/t/bookending-time-range/154533/4 "2018-10-30T17:32:36Z")

</div>

As as said: an aggregation max and an aggregation min on `timestamp_value`?

---

<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 27, 2018, 5:32pm UTC](https://discuss.elastic.co/t/bookending-time-range/154533/5 "2018-11-27T17:32:43Z")

</div>

Closed on November 27, 2018, 5:32pm UTC

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