# Compare fields in two documents?

**URL:** <https://discuss.elastic.co/t/compare-fields-in-two-documents/116548>\
**Category:** Kibana\
**Created:** [January 22, 2018, 10:49pm UTC](https://discuss.elastic.co/t/compare-fields-in-two-documents/116548 "2018-01-22T22:49:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyle1441](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@kyle1441](https://discuss.elastic.co/u/kyle1441)\
**Post date:** [January 22, 2018, 10:49pm UTC](https://discuss.elastic.co/t/compare-fields-in-two-documents/116548/1 "2018-01-22T22:49:57Z")

</div>

Hello, In this topic, [Get time elapsed between two different documents (now locked)](https://discuss.elastic.co/t/get-time-elapsed-between-two-different-documents/55935), it is indicated that Scripted Fields can help with this. However, elsewhere I've seen it mentioned that scripted fields only allow field comparisons between fields within the same document.

Can someone please clarify if this is possible? If not, any recommendations that would allow this?

One thought I had was to create a new document which contained the relevant fields from the earlier documents, any thoughts or recommendations for doing so?

Use case:  
Document 1: Application started, user id, session id, timestamp this occured  
Document 2: Application exited, user id, session id, timestamp this occured  
Objective: Calculate time between these documents, later create graphs to look at average session duration, etc.

Thank you!

---

<div class="post-metadata">

**Author:** ![timroes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timroes/32/19712_2.png) [@timroes](https://discuss.elastic.co/u/timroes)\
**Post date:** [January 23, 2018, 10:00am UTC](https://discuss.elastic.co/t/compare-fields-in-two-documents/116548/2 "2018-01-23T10:00:24Z")

</div>

Scripted fields can only calculate data WITHIN one document. You CANNOT calculate data across multiple documents.

To achieve writing that diff, you will need to measure it before indexing the documents, so that you write the time diff to the ending document.

In Logstash e.g. the [elapsed](https://www.elastic.co/guide/en/logstash/current/plugins-filters-elapsed.html) plugin can achieve that.

Cheers,  
Tim

---

<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:** [February 20, 2018, 10:00am UTC](https://discuss.elastic.co/t/compare-fields-in-two-documents/116548/3 "2018-02-20T10:00:27Z")

</div>

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