Chapter 28. Looking at Time
If search is the most popular activity in Elasticsearch, building date histograms must be the second most popular. Why would you want to use a date histogram?
Imagine your data has a timestamp. It doesn’t matter what the data is—Apache log events, stock buy/sell transaction dates, baseball game times—anything with a timestamp can benefit from the date histogram. When you have a timestamp, you often want to build metrics that are expressed over time:
-
How many cars sold each month this year?
-
What was the price of this stock for the last 12 hours?
-
What was the average latency of our website every hour in the last week?
While regular histograms are often represented as bar charts, date histograms
tend to be converted into line graphs representing time series. Many
companies use Elasticsearch solely for analytics over time series data. The date_histogram
bucket is their bread and butter.
The date_histogram
bucket works similarly to the regular histogram
. Rather
than building buckets based on a numeric field representing numeric ranges,
it builds buckets based on time ranges. Each bucket is therefore defined as a
certain calendar size (for example, 1 month
or 2.5 days
).
Get Elasticsearch: The Definitive Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.