Elasticsearch query on date?

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.

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"
               }
            }
         }
      }
   }
}

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

you may want to see this page.

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

Have a look at the date histogram aggregation where you can set the interval to "1d". This would give you the counts datewise as requested.

HTH

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

@balumurari1 thnaks :slight_smile: appreciate it .

@Abhilash_B and @jasony thanks for the tip.

peace :sunglasses:

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