
Elasticsearch 8.x Cookbook
By :

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.
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.
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:
PUT test-histo { "mappings": { "properties": { "histogram": { "type": "histogram" }, "model": { "type": "keyword" } } } }
POST test-histo/_doc/1 { "model":"show_level", "histogram" : { "values" : [2016, 2017, 2018, 2019, 2020, 2021], "counts" : [283, 337, 323, 312, 236, 232] } }
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:
min
, max
, sum
, value_count
, and avg
percentiles
and percentile_ranks
aggregationsboxplot
aggregationhistogram
aggregationThe data is not indexed, but you can also check the existence of a document by populating this field with the exist
query.