Elastic search get one record per hour Elastic search get one record per hour elasticsearch elasticsearch

Elastic search get one record per hour


If you are grouping docs by hour, then for every interval you already know how many results are going to be.

For the interval you provided (28-08-2018 - 15-09-2018), there's 432 hours in between, so you know that there's going to be 432 results. Now, lets say you want to show 8 results per page, that means there's going to be 54 pages, where every page is going to contain results from 8 hour window:

1st page is 28-08-2018 00:00:00 (1535410800000) - 28-08-2018 08:00:00 (1535410800000 + 8 * 3600000)so your aggregation request for the first page should look like:

...  "range": {    "createdtime": {      "gte": "1535410800000",      "lt": "1535439600000",      "boost": 2.0    }  }...

2nd page is 28-08-2018 08:00:00 (1535410800000 + 8 * 3600000) - 28-08-2018 16:00:00 (1535410800000 + 16 * 3600000)

...  "range": {    "createdtime": {      "gte": "1535439600000",      "lt": "1535468400000",      "boost": 2.0    }  }...

and so on, you just narrow down your aggregation based on createdtime and that will return results for particular page.