Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch count in groups by date range

I have documents like this:

{
body: 'some text',
read_date: '2017-12-22T10:19:40.223000'
}

Is there a way to query count of documents published in last 10 days group by date? For example:

2017-12-22, 150  
2017-12-21, 79  
2017-12-20, 111  
2017-12-19, 27  
2017-12-18, 100  
like image 844
ehsan shirzadi Avatar asked Dec 22 '17 08:12

ehsan shirzadi


People also ask

How do I count records in Elasticsearch?

The count API allows you to execute a query and get the number of matches for that query. The query can either be provided using a simple query string as a parameter, or using the Query DSL defined within the request body.

What is range query in Elasticsearch?

Range Queries in Elasticsearch Combining the greater than ( gt ) and less than ( lt ) range parameters is an effective way to search for documents that contain a certain field value within a range where you know the upper and lower bounds. In this example, we can find all cars that were made in 2016, 2017, and 2018: 1.

What is date histogram?

Date histogram aggregationedit. This multi-bucket aggregation is similar to the normal histogram, but it can only be used with date or date range values. Because dates are represented internally in Elasticsearch as long values, it is possible, but not as accurate, to use the normal histogram on dates as well.


2 Answers

Yes, you can easily achieve that using a date_histogram aggregation, like this:

{
  "query": {
    "range": {
      "read_date": {
        "gte": "now-10d"
      }
    }
  },
  "aggs": {
    "byday": {
      "date_histogram": {
        "field": "read_date",
        "interval": "day"
      }
    }
  }
}
like image 141
Val Avatar answered Nov 10 '22 05:11

Val


To receive day count of the past 10 days, per day you can POST the following query:

{
  "query": {
    "range": {
      "read_date": {
        "gte": "now-11d/d",
        "lte": "now-1d/d"
      }
    }
  },

    "aggs" : {
        "byDay" : {
            "date_histogram" : {
                "field" : "read_date",
                "calendar_interval" : "1d",
                "format" : "yyyy-MM-dd" 
            }
        }
    }
}

To the following Url: http://localhost:9200/Index_Name/Index_Type/_search?size=0

Setting size to 0 avoids executing the fetch phase of the search making the request more efficient. See this elastic documentation for more information.

like image 37
Mark Avatar answered Nov 10 '22 05:11

Mark