# Calculate differences between documents in a query

**URL:** <https://discuss.elastic.co/t/calculate-differences-between-documents-in-a-query/351519>\
**Category:** Elasticsearch\
**Created:** [January 22, 2024, 9:03am UTC](https://discuss.elastic.co/t/calculate-differences-between-documents-in-a-query/351519 "2024-01-22T09:03:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbby](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@mbby](https://discuss.elastic.co/u/mbby)\
**Post date:** [January 22, 2024, 9:03am UTC](https://discuss.elastic.co/t/calculate-differences-between-documents-in-a-query/351519/1 "2024-01-22T09:03:44Z")

</div>

I need to calculate the MTBF of our systems which are monitored using the observatiblity uptime feature of Elasticsearch.

Let's say that I've an index with the following values:  
time, system-name, state  
10:00, system1, up  
10:05, system1, up  
10:10, system1, down  
10:15, system1, down  
10:20, system1, up  
10:25, system1, down  
10:30, system1, down  
10:35, system1, down  
10:40, system1, up

How could I count the number of times the state switched from down to up (here: 2).  
How could I get the time between two of those state changes (here: 10:35-10:15 = 0:20).

---

<div class="post-metadata">

**Author:** ![mbby](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@mbby](https://discuss.elastic.co/u/mbby)\
**Post date:** [January 24, 2024, 1:12pm UTC](https://discuss.elastic.co/t/calculate-differences-between-documents-in-a-query/351519/2 "2024-01-24T13:12:25Z")

</div>

So one of the questions is: is there a query which calculates difference between consecutive documents. E.g.:  
time, spent money  
10:00, 100  
10:05, 105  
10:10, 150

the result should be something like, 105-100=5, 150-105=45

---

<div class="post-metadata">

**Author:** ![mbby](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@mbby](https://discuss.elastic.co/u/mbby)\
**Post date:** [February 6, 2024, 6:29am UTC](https://discuss.elastic.co/t/calculate-differences-between-documents-in-a-query/351519/3 "2024-02-06T06:29:19Z")

</div>

It looks like this is not possible in an Elasticsearch query itself, right?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [February 6, 2024, 2:55pm UTC](https://discuss.elastic.co/t/calculate-differences-between-documents-in-a-query/351519/4 "2024-02-06T14:55:39Z")

</div>

> [@mbby](#):
>
> So one of the questions is: is there a query which calculates difference between consecutive documents. E.g.:  
> time, spent money  
> 10:00, 100  
> 10:05, 105  
> 10:10, 150

Perhaps

> **[Serial differencing aggregation | Elasticsearch Guide \[8.12\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-serialdiff-aggregation.html)**

You can do serial differencing in Lens as well.

---

<div class="post-metadata">

**Author:** ![mbby](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@mbby](https://discuss.elastic.co/u/mbby)\
**Post date:** [February 7, 2024, 7:23am UTC](https://discuss.elastic.co/t/calculate-differences-between-documents-in-a-query/351519/5 "2024-02-07T07:23:55Z")

</div>

Thanks Stephen. Found that and tried it. Unfortunately this won't solve my initial problem of getting the distances between two state changes.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 6, 2024, 7:24am UTC](https://discuss.elastic.co/t/calculate-differences-between-documents-in-a-query/351519/6 "2024-03-06T07:24:18Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
