# Get top n with date histogram

**URL:** https://discuss.elastic.co/t/get-top-n-with-date-histogram/222843
**Category:** Elasticsearch
**Created:** [March 10, 2020, 3:30am UTC](https://discuss.elastic.co/t/get-top-n-with-date-histogram/222843 "2020-03-10T03:30:43Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ming1997](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@ming1997](https://discuss.elastic.co/u/ming1997)
#### Post date: [March 10, 2020, 3:30am UTC](https://discuss.elastic.co/t/get-top-n-with-date-histogram/222843/1 "2020-03-10T03:30:44Z")

</div>

"aggs": {  
"2": {  
"date\_histogram": {  
"field": "@timestamp",  
"interval": "30s",  
"time\_zone": "Asia/Shanghai",  
"min\_doc\_count": 1  
},  
"aggs": {  
"3": {  
"terms": {  
"field": "flow.src\_addr.keyword",  
"order": {  
"\_count": "desc"  
},  
"size": 5  
}  
}  
}  
}  
I want to show top 5 ip with time series like the kibana visual builder.  
But this aggregation can onliy get top 5 ip in every intervals.  
I want only 5 ip with the date histogram , not every intervals.

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [March 10, 2020, 1:22pm UTC](https://discuss.elastic.co/t/get-top-n-with-date-histogram/222843/2 "2020-03-10T13:22:23Z")

</div>

if you are only interested in the top 5, then why not use a terms aggregation on the src address field? What am I missing that is not covered by that? Or do you want to take the intervals into account? Some more context except a query JSON would be helpful.

Thanks!

---

<div class="post-metadata">

### Author: ![ming1997](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@ming1997](https://discuss.elastic.co/u/ming1997)
#### Post date: [March 13, 2020, 6:05am UTC](https://discuss.elastic.co/t/get-top-n-with-date-histogram/222843/3 "2020-03-13T06:05:16Z")

</div>

Thanks for your reply. Actually, i want to the top 10 ip in the nearest interval. And then show the 10 ip goup by every 5 minutes. It seems that i have to make two queries.  
I used kibana visual builder before. It worked like this. So I wonder if I can make it in one query.

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [March 13, 2020, 8:35am UTC](https://discuss.elastic.co/t/get-top-n-with-date-histogram/222843/4 "2020-03-13T08:35:53Z")

</div>

Hey,

you can have several aggregations in one search request. Is this for a visualization or for yourself? Just to make sure I understand. the request...

--Alex

---

<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: [April 10, 2020, 8:35am UTC](https://discuss.elastic.co/t/get-top-n-with-date-histogram/222843/5 "2020-04-10T08:35:53Z")

</div>

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