# Newbie query to return a single doc from every 4 hours of data

**URL:** https://discuss.elastic.co/t/newbie-query-to-return-a-single-doc-from-every-4-hours-of-data/147277
**Category:** Elasticsearch
**Created:** [September 4, 2018, 10:28pm UTC](https://discuss.elastic.co/t/newbie-query-to-return-a-single-doc-from-every-4-hours-of-data/147277 "2018-09-04T22:28:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Mark\_Lloyd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_lloyd/32/34827_2.png) [@Mark\_Lloyd](https://discuss.elastic.co/u/Mark_Lloyd)
#### Post date: [September 4, 2018, 10:28pm UTC](https://discuss.elastic.co/t/newbie-query-to-return-a-single-doc-from-every-4-hours-of-data/147277/1 "2018-09-04T22:28:42Z")

</div>

Hi All

I'm very new to elastic search. I have an index that is indexing data received by ES every 10 seconds of everyday.

Now I want to query that data and filter it on some fields (got that sorted) and return data in groups, lets say groups of every 4 hours for past 24 hrs. I dont want to sum or avg anything in particular.

I want to return the closest single doc to each interval to basically build up some data points for use in a graph.

I'm looking at aggs but that only returns buckets with document counts.

I'm not even sure what term or concept I should be looking for in the docs.

Can anyone help out there?

Cheers

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [September 7, 2018, 9:50am UTC](https://discuss.elastic.co/t/newbie-query-to-return-a-single-doc-from-every-4-hours-of-data/147277/2 "2018-09-07T09:50:36Z")

</div>

I think the [top\_hits aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html) may be what you're looking for. This is an aggregation that you can nest in a bucket aggregation (in your case a date\_histogram aggregation with a 4 hour interval). The top\_hits aggregation then shows you one or more documents in each of those buckets, based on whatever sorting criteria you apply.

---

<div class="post-metadata">

### Author: ![Mark\_Lloyd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_lloyd/32/34827_2.png) [@Mark\_Lloyd](https://discuss.elastic.co/u/Mark_Lloyd)
#### Post date: [September 8, 2018, 11:28pm UTC](https://discuss.elastic.co/t/newbie-query-to-return-a-single-doc-from-every-4-hours-of-data/147277/3 "2018-09-08T23:28:52Z")

</div>

Many thanks for the tip

I’ll be trying it out this coming week

Appreciated

---

<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 6, 2018, 11:29pm UTC](https://discuss.elastic.co/t/newbie-query-to-return-a-single-doc-from-every-4-hours-of-data/147277/4 "2018-10-06T23:29:03Z")

</div>

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