🏡 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.

Use Case: Percentual Changes in Aggregated Coverage

  1. I'm an insurance portfolio manager and want to know by how much the total contractual coverage (also called "sum insured") changed in 2020 compared to 2019 — both in $$$ and in percent.
  2. On top of that, I want to explore these YoY trends in two separate bands: where the coverage is < $1M and where it's ≥ $1M.

At the end of the day, I'm requesting a table looking like this:

Aggregated Coverage Trends — 2020 vs. 2019

Approach: Bucket Scripts

Let's assume a highly simplified insurance contract of the form:

{
  "created_at": "2019/03/12 07:21:17",
  "coverage": 570000
}

Next, we'll leverage the facts that:

  1. row-based calculations can be achieved through [filters](<https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html>) aggregations as outlined here
  2. and that a [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: