# Compute distance between current and previous value

**URL:** <https://discuss.elastic.co/t/compute-distance-between-current-and-previous-value/91902>\
**Category:** Elasticsearch\
**Created:** [July 5, 2017, 12:50pm UTC](https://discuss.elastic.co/t/compute-distance-between-current-and-previous-value/91902 "2017-07-05T12:50:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pksingla](https://avatars.discourse-cdn.com/v4/letter/p/c2a13f/32.png) [@pksingla](https://discuss.elastic.co/u/pksingla)\
**Post date:** [July 5, 2017, 12:50pm UTC](https://discuss.elastic.co/t/compute-distance-between-current-and-previous-value/91902/1 "2017-07-05T12:50:40Z")

</div>

I have CPU Utilization data ingested into Elastic Search. My requirement is to calculate distance between 2 subsequent values e.g. current value and previous value. Please provide any pointers to achieve the same using script Query in Elastic Search.

Below is the snapshot of sample data ingested into ES. Currently I have single server with single metric only.

2017-07-2 11:47:40,server1,processor(\_total)-% processor time,2.86,System Processor Time,w3wp,APM2  
2017-07-2 11:48:10, server1,processor(\_total)-% processor time,7.11,System Processor Time,w3wp,APM2  
2017-07-2 11:48:40, server1,processor(\_total)-% processor time,3.04,System Processor Time,w3wp,APM2  
2017-07-2 11:49:10, server1,processor(\_total)-% processor time,3.37,System Processor Time,w3wp,APM2

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [July 5, 2017, 1:45pm UTC](https://discuss.elastic.co/t/compute-distance-between-current-and-previous-value/91902/2 "2017-07-05T13:45:13Z")

</div>

What do you want to do with this distance? You should probably compute that distance on the client side before sending the data to Elasticsearch. Once documents are in Elasticsearch, the order of documents does not mean much, Elasticsearch actually freely reorders documents if it allows for more efficient merges.

---

<div class="post-metadata">

**Author:** ![pksingla](https://avatars.discourse-cdn.com/v4/letter/p/c2a13f/32.png) [@pksingla](https://discuss.elastic.co/u/pksingla)\
**Post date:** [July 5, 2017, 4:56pm UTC](https://discuss.elastic.co/t/compute-distance-between-current-and-previous-value/91902/3 "2017-07-05T16:56:20Z")

</div>

Thanks for the response. I cannot do processing on the client side due to some restrictions.

I have metric\_time field stored in ES which can be used to get current and previous value but I am not getting any pointers how to achieve the same in query in ES. Pls let me know if you need more info on the requirements.

---

<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:** [August 2, 2017, 4:56pm UTC](https://discuss.elastic.co/t/compute-distance-between-current-and-previous-value/91902/4 "2017-08-02T16:56:40Z")

</div>

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