# Elasticsearch query on date?

**URL:** https://discuss.elastic.co/t/elasticsearch-query-on-date/163343
**Category:** Elasticsearch
**Created:** [January 8, 2019, 10:39am UTC](https://discuss.elastic.co/t/elasticsearch-query-on-date/163343 "2019-01-08T10:39:05Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![inandi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/inandi/32/40801_2.png) [@inandi](https://discuss.elastic.co/u/inandi)
#### Post date: [January 8, 2019, 10:39am UTC](https://discuss.elastic.co/t/elasticsearch-query-on-date/163343/1 "2019-01-08T10:39:05Z")

</div>

I have an index where **DateTime** field is there in this format **2018-01-02T18:24:39.379Z**. now this is date and time both, I want to get query where I am able to get the group by data on only **date** filter.

sample data

```
  {
    "_index" : "review_location_demo_21",
    "_type" : "doc",
    "_id" : "AIe9_BHx9wW5OAXpNIH08blLdli0qMjUTeI0Qy6RUVZwoSiwiEodHJMIXCuNaOzYFPU-T8iu8PJP3K5m_vtGJyRNtx5VVX3hvlfpqUwGQwoEgt4C4NAB0To",
    "_score" : 1.0,
    "_source" : {
      "locationId" : "2903735955624913227",
      "createTime" : "2018-08-29T04:22:23.103743Z",
    }
  },
  {
    "_index" : "review_location_demo_21",
    "_type" : "doc",
    "_id" : "AIe9_BFhqAtkXvUqdYNeMuBBGjaANCiK_fz0Nk6WFHWMFogV7-IIZai7XqIzbFOz5bHT29RKVn4QI-K1iRibHpR2P4x5oy6yWmth3X7giVY56bHZ7PeMYI0",
    "_score" : 1.0,
    "_source" : {
      "locationId" : "2903735955624913227",
       "createTime" : "2019-01-04T14:04:23.141731Z"
    }
  }

```

suppose now i want count of data on date basis.

---

<div class="post-metadata">

### Author: ![balumurari1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/balumurari1/32/39203_2.png) [@balumurari1](https://discuss.elastic.co/u/balumurari1)
#### Post date: [January 8, 2019, 10:48am UTC](https://discuss.elastic.co/t/elasticsearch-query-on-date/163343/2 "2019-01-08T10:48:38Z")

</div>

As per my knowledge, we can frame query to filter data like fromdate to todate, which is the better idea than to group by date like,

```
GET indexname/_doc/_search
{
   "query":{
      "bool":{
         "must":{
            "match":{
               "key1":"sample"
            }
         },
         "filter":{
            "range":{
               "created_date":{
                  "gte":"2018-08-01T18:30:00.000Z",
                  "lt":"2018-11-01T18:30:00.000Z"
               }
            }
         }
      }
   }
}
```

---

<div class="post-metadata">

### Author: ![inandi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/inandi/32/40801_2.png) [@inandi](https://discuss.elastic.co/u/inandi)
#### Post date: [January 8, 2019, 10:55am UTC](https://discuss.elastic.co/t/elasticsearch-query-on-date/163343/3 "2019-01-08T10:55:51Z")

</div>

thanks for reply @balumurari1 , but i checked this query and not working. i want to get datewise data count like

2018/12/01 i may get 4 documents ,  
2018/12/02 i may get 2 documents ,  
2018/12/03 i may get 3 documents ,

i need something like this.

thank you again in advance.

@inandi

---

<div class="post-metadata">

### Author: ![jasony](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jasony](https://discuss.elastic.co/u/jasony)
#### Post date: [January 8, 2019, 11:20am UTC](https://discuss.elastic.co/t/elasticsearch-query-on-date/163343/4 "2019-01-08T11:20:10Z")

</div>

you may want to see this page.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html)

---

<div class="post-metadata">

### Author: ![Abhilash\_B](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhilash_b/32/40270_2.png) [@Abhilash\_B](https://discuss.elastic.co/u/Abhilash_B)
#### Post date: [January 8, 2019, 11:20am UTC](https://discuss.elastic.co/t/elasticsearch-query-on-date/163343/5 "2019-01-08T11:20:21Z")

</div>

Have a look at the [date histogram aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html) where you can set the interval to "1d". This would give you the counts datewise as requested.

HTH

---

<div class="post-metadata">

### Author: ![balumurari1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/balumurari1/32/39203_2.png) [@balumurari1](https://discuss.elastic.co/u/balumurari1)
#### Post date: [January 8, 2019, 11:23am UTC](https://discuss.elastic.co/t/elasticsearch-query-on-date/163343/6 "2019-01-08T11:23:23Z")

</div>

> [@inandi](#):
>
> but i checked this query and not working. i want to get datewise data count

Yup, that query will give the data within the range specified in the query.

---

<div class="post-metadata">

### Author: ![inandi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/inandi/32/40801_2.png) [@inandi](https://discuss.elastic.co/u/inandi)
#### Post date: [January 8, 2019, 11:30am UTC](https://discuss.elastic.co/t/elasticsearch-query-on-date/163343/7 "2019-01-08T11:30:43Z")

</div>

@balumurari1 thnaks 🙂 appreciate it .

@Abhilash_B and @jasony thanks for the tip.

peace 🕶

---

<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: [February 5, 2019, 11:30am UTC](https://discuss.elastic.co/t/elasticsearch-query-on-date/163343/8 "2019-02-05T11:30:49Z")

</div>

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