🏡 Home 📖 Chapter Home 👉 Next
⚡ ElasticsearchBook is crafted by Jozef Sorocin (🟢 Book a consulting hour) 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
aggregations as outlined herebucket_script
aggregation can execute a script which performs per bucket computations on specified metrics. In our case, the buckets will be results of sum
aggregations on a contract's coverage
field.In pseudo-code, we're therefore looking at something along the lines of: