Bookending time range


(David Story) #1

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!


(David Pilato) #2

A min and max aggregation on the date field?


(David Story) #3

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!


(David Pilato) #4

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


(system) #5

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