# Elastic search get one record per hour

**URL:** https://discuss.elastic.co/t/elastic-search-get-one-record-per-hour/148327
**Category:** Elasticsearch
**Created:** [September 12, 2018, 1:01pm UTC](https://discuss.elastic.co/t/elastic-search-get-one-record-per-hour/148327 "2018-09-12T13:01:44Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Jothi\_Kannan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jothi_kannan/32/35491_2.png) [@Jothi\_Kannan](https://discuss.elastic.co/u/Jothi_Kannan)
#### Post date: [September 12, 2018, 1:01pm UTC](https://discuss.elastic.co/t/elastic-search-get-one-record-per-hour/148327/1 "2018-09-12T13:01:44Z")

</div>

I want to get only one record per hours in a date range. For Example, If I want to get the data's between the days (28-08-2018 - 15-09-2018) there are almost 10000 records, but I want to filter the result to show only one record per hour, So I am using aggregation with date\_histogram, and I can see only one record per hour

```
{ 
    "size" : 0,
      "query": {
        "bool": {
          "must": [
                    {
              "range": {
                "createdtime": {
                  "gte": "1535201500000",
                  "lte": "1536756706000",
                  "boost": 2.0
                }
              }
            },

            {
              "match": {
                "gen": 1
              }
            },
            {
              "match": {
                "Mid": 350404
              }
            }
          ]
        }
      },

    "aggregations" : {
        "runtime" : {
            "date_histogram" : {
                "field" : "createdtime",
                "interval" : "1H",
                "min_doc_count": 1
            },"aggs": {
                "tops": {
                  "top_hits": {
                    "size": 1
                  }
                }
            }
        }
    }      
}

```

Here the problem is I want to use pagination to show these results as I know there is no way to use size and from on aggregation query as of now, I want to know is there any other way to get only one records in an hour

---

<div class="post-metadata">

### Author: ![jaddison](https://avatars.discourse-cdn.com/v4/letter/j/e5b9ba/32.png) [@jaddison](https://discuss.elastic.co/u/jaddison)
#### Post date: [September 12, 2018, 3:42pm UTC](https://discuss.elastic.co/t/elastic-search-get-one-record-per-hour/148327/2 "2018-09-12T15:42:07Z")

</div>

I think you could do this using [field collapsing](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html)?

You would probably need to index a `date_hour` field of `keyword` type. Format it like `yearMONTHdayHOUR` (ie. **2018091209** ).

With this approach (if it works at all!), you wouldn't need aggregations at all. I'd love to know if this works, by the way. I haven't had a chance to play with collapsing yet.

---

<div class="post-metadata">

### Author: ![Jothi\_Kannan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jothi_kannan/32/35491_2.png) [@Jothi\_Kannan](https://discuss.elastic.co/u/Jothi_Kannan)
#### Post date: [September 14, 2018, 10:01am UTC](https://discuss.elastic.co/t/elastic-search-get-one-record-per-hour/148327/3 "2018-09-14T10:01:21Z")

</div>

@jaddison, thanks for the kind reply.

But I think Field collapsing won't work in my case as I am getting the records by hours, in my example just I have been using **1H** , but it may vary to **1 to 24H**.

Please advice me on any other technic

---

<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: [October 12, 2018, 10:01am UTC](https://discuss.elastic.co/t/elastic-search-get-one-record-per-hour/148327/4 "2018-10-12T10:01:22Z")

</div>

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