🏡 Home 📖 Chapter Home 👉 Next
⚡ ElasticsearchBook.com is crafted by Jozef Sorocin and powered by:
In a variety of circumstances you may want to calculate trends — how a metric is changing with respect to some baseline value. Let's look at a typical insurance use case.
At the end of the day, I'm requesting a table looking like this:
Aggregated Coverage Trends — 2020 vs. 2019
Let's assume a highly simplified insurance contract of the form:
{
"created_at": "2019/03/12 07:21:17",
"coverage": 570000
}
portfolio
index mapping could then look like:Next, we'll leverage the facts that:
[filters](<https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html>)
aggregations as outlined here[bucket_script](<https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html>)
aggregation can execute a script which performs per bucket computations on specified metrics. In our case, the buckets will be results of [sum](<https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-sum-aggregation.html>)
aggregations on a contract's coverage
field.In pseudo-code, we're therefore looking at something along the lines of: