Using the Histogram field type
Histograms are a common data type for analytics and machine learning analysis. We can store Histograms in the form of values and counts; they are not indexed, but they can be used in aggregations.
The histogram
field type is a special mapping that's available in X-Pack that is commonly used to store the results of Histogram aggregations in Elasticsearch for further processing, such as to compare the aggregation results at different times.
Getting ready
You will need an up-and-running Elasticsearch installation, as described in the Downloading and installing Elasticsearch recipe of Chapter 1, Getting Started.
To execute the commands in this recipe, you can use any HTTP client, such as curl (https://curl.haxx.se/), Postman (https://www.getpostman.com/), or similar. I suggest using the Kibana console, which provides code completion and better character escaping for Elasticsearch.
How to do it…
In this recipe, we will simulate a common use case of Histogram data that is stored in Elasticsearch. Here, we will use a Histogram that specifies the millimeters of rain divided by year for our advanced analytics solution. To achieve this, follow these steps:
- First, let's create an index for the Histogram by using the following mapping:
PUT test-histo { "mappings": { "properties": { "histogram": { "type": "histogram" }, "model": { "type": "keyword" } } } }
- Now, we can store a document to test the mapping:
POST test-histo/_doc/1 { "model":"show_level", "histogram" : { "values" : [2016, 2017, 2018, 2019, 2020, 2021], "counts" : [283, 337, 323, 312, 236, 232] } }
How it works…
The histogram
field type specializes in storing Histogram data. I must be provided as a JSON object composed of the values
and counts
fields with the same cardinality of items. The only supported aggregations are the following ones. We will look at these in more detail in Chapter 7, Aggregations:
- Metric aggregations such as
min
,max
,sum
,value_count
, andavg
- The
percentiles
andpercentile_ranks
aggregations - The
boxplot
aggregation - The
histogram
aggregation
The data is not indexed, but you can also check the existence of a document by populating this field with the exist
query.
See also
- Aggregations will be discussed in more detail in Chapter 7, Aggregations
- The Using the exist query recipe in Chapter 5, Text and Numeric Queries