# Date histogram query; count values

**URL:** https://discuss.elastic.co/t/date-histogram-query-count-values/131279
**Category:** Elasticsearch
**Created:** [May 10, 2018, 9:42am UTC](https://discuss.elastic.co/t/date-histogram-query-count-values/131279 "2018-05-10T09:42:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jfdsa](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@jfdsa](https://discuss.elastic.co/u/jfdsa)
#### Post date: [May 10, 2018, 9:42am UTC](https://discuss.elastic.co/t/date-histogram-query-count-values/131279/1 "2018-05-10T09:42:07Z")

</div>

Hello, I am new to elastic, I am having some problems with queries.

I would like to do a date-histogram aggregation like query, where I can specify a time interval and get count values, but for specific values of a field. For example, I want a counter that indicates how many times an ip appears in the field "src\_ip" for each time interval, specifying start and end to the search windows. I don't know how to combine buckets to get this result.

I can do it on kibana, but I want to get a query to integrate in a python program, but I can't figure out how to do it.

---

<div class="post-metadata">

### Author: ![shanec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shanec/32/4004_2.png) [@shanec](https://discuss.elastic.co/u/shanec)
#### Post date: [May 10, 2018, 2:00pm UTC](https://discuss.elastic.co/t/date-histogram-query-count-values/131279/2 "2018-05-10T14:00:22Z")

</div>

What you're looking for is a [date histogram](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html) and inside of that nest an [IP range aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-iprange-aggregation.html) or, if you want an approximate count of individual IP addresses, a [terms aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html) or, if you want an exhaustive list of _all_ IP addresses, a [composite aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html).

Here's a tip as well: if you know how to get a visualization in Kibana and want to learn how to build a similar query, you can do that!

Open your visualization and find the up arrow at the bottom and click it:

 ![15%20AM](https://us1.discourse-cdn.com/elastic/original/3X/9/3/9346e15f65e5c92891576ed2281e49e79c891281.png)

You'll see this tabular view of the data. You can export the raw data here, but what we're interested is getting the Elasticsearch request, so click that

 ![16%20AM](https://us1.discourse-cdn.com/elastic/original/3X/d/8/d8f51d390fb8470d676528631d33699510f76616.png)

Tada! You now have an Elasticsearch query that you can use as the building blocks for your own.

 ![46%20AM](https://us1.discourse-cdn.com/elastic/original/3X/c/b/cb84583066495a93dc2bd1132cbfeb512d6d6d08.jpg)

Kibana sometimes adds some extra "stuff" that you may not need, so you can search the Elasticsearch documentation on our website to see what things like [this `excludes` thing under `_source` does](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/mapping-source-field.html#include-exclude).

---

<div class="post-metadata">

### Author: ![jfdsa](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@jfdsa](https://discuss.elastic.co/u/jfdsa)
#### Post date: [May 11, 2018, 6:44am UTC](https://discuss.elastic.co/t/date-histogram-query-count-values/131279/3 "2018-05-11T06:44:25Z")

</div>

Thanks!, I think this is what I was looking for, however, that kibana to request option was really helpful.

---

<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: [June 8, 2018, 6:44am UTC](https://discuss.elastic.co/t/date-histogram-query-count-values/131279/4 "2018-06-08T06:44:31Z")

</div>

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