Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show day names,using date-histogram aggregation in elascticsearch

I have been trying to use date histogram aggregation in elasticsearch and it returns the date as epoch or in yy-mm-dd-mm-ss format. But what I want is to get document count per week day like monday , tuesday etc. Is there any way in which I can do that?

like image 922
Simi Avatar asked Mar 12 '15 04:03

Simi


People also ask

What is date histogram in Elasticsearch?

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.

Is Elasticsearch good for aggregations?

Elasticsearch Aggregations provide you with the ability to group and perform calculations and statistics (such as sums and averages) on your data by using a simple search query. An aggregation can be viewed as a working unit that builds analytical information across a set of documents.

What is Bucket aggregation in Elasticsearch?

Bucket aggregations don't calculate metrics over fields like the metrics aggregations do, but instead, they create buckets of documents. Each bucket is associated with a criterion (depending on the aggregation type) which determines whether or not a document in the current context "falls" into it.


2 Answers

I may be missing something, but, isn't the answer simpler than Vineeth's answer?

"aggregations": {
    "timeslice": {
        "histogram": {
            "script": "doc['timestamp'].date.getHourOfDay()",
            "interval": 1,
            "min_doc_count": 0,
            "extended_bounds": {
                "min": 0,
                "max": 23
            },
            "order": {
                "_key": "desc"
            }
        }
    }

This is nice, as it'll also include any hours with zero results, and, it'll extend the results to cover the entire 24 hour period (due to the extended_bounds).

You can use 'getDayOfWeek', 'getHourOfDay', ... (see 'Joda time' for more).

This is great for hours, but for days / months it'll give you an number rather than the month name. To work around, you can get the timeslot as a string - but, this'll won't work with the extended bounds approach, so you may have empty results (i.e. [Mon, Tues, Fri, Sun]).

In-case you want that, it is here:

"aggregations": {
    "dayOfWeek": {
        "terms": {
            "script": "doc['timestamp'].date.dayOfWeek().getAsText()",
            "order": {
                "_term": "asc"
            }
        }
    }

Even if this doesn't help you, hopefully someone else will find it and benefit from it.

like image 163
RichS Avatar answered Sep 22 '22 15:09

RichS


You need to go for a different approach. Using scripts , you can convert date time into week day. On this value if you apply terms aggregation , it should work fine.

Script to convert date time value into weekday

Date date = new Date(doc['created_at'].value) ; 
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat('EEE');
format.format(date)

Query to get the values

{
  "aggs": {
    "perWeekDay": {
      "terms": {
        "script": "Date date = new Date(doc['created_at'].value) ; java.text.SimpleDateFormat format = new java.text.SimpleDateFormat('EEE');format.format(date)"
      }
    }
  }
}

You can also find some more examples on using scripting in aggregations here.

like image 27
Vineeth Mohan Avatar answered Sep 19 '22 15:09

Vineeth Mohan