Bookending time range

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!

A min and max aggregation on the date field?

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!

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

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