# Combining two documents within the same index

**URL:** <https://discuss.elastic.co/t/combining-two-documents-within-the-same-index/201857>\
**Category:** Elasticsearch\
**Created:** [October 1, 2019, 7:40pm UTC](https://discuss.elastic.co/t/combining-two-documents-within-the-same-index/201857 "2019-10-01T19:40:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishal\_M](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vishal_m/32/50559_2.png) [@vishal\_M](https://discuss.elastic.co/u/vishal_M)\
**Post date:** [October 1, 2019, 7:40pm UTC](https://discuss.elastic.co/t/combining-two-documents-within-the-same-index/201857/1 "2019-10-01T19:40:45Z")

</div>

Hello Team,

I have a use-case of combining two documents (which has a similar timestamp for both the records) based on a value of a particular field. Please go through the data pic in detail.

 ![20191001_152439](https://us1.discourse-cdn.com/elastic/original/3X/4/4/44dc5bb8e4582e6c9d04673fb4b3723e9c4977ec.jpeg)

If you observe the data in line no 1 and line number 4, the device value and remote value are interchanged(client - server flow & server client flow). I want to display similar output as line 5 (based on the value of the device field in one document, I want to check the value of Remote. If both are the same(equal), I want to display (client - server flow )device, remote, app, protocol and add the values of bytes for the initial record and matched record).

I can do it by adding extra logic in an API, process it and push it to the ES index as the way I wanted. But, I want to handle it in the Kibana side.

Is there any way I can do it in Kibana by writing script fields on top of the index pattern in Kibana (Index Management) / any alternative methods?

Thanks in advance ..

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 4, 2019, 9:49am UTC](https://discuss.elastic.co/t/combining-two-documents-within-the-same-index/201857/2 "2019-10-04T09:49:18Z")

</div>

I'd use a script to create a single concatenated string from the 2 values, ensuring they are sorted otherwise you get `A_B` and `B_A` as 2 different terms.

---

<div class="post-metadata">

**Author:** ![vishal\_M](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vishal_m/32/50559_2.png) [@vishal\_M](https://discuss.elastic.co/u/vishal_M)\
**Post date:** [October 4, 2019, 1:18pm UTC](https://discuss.elastic.co/t/combining-two-documents-within-the-same-index/201857/3 "2019-10-04T13:18:04Z")

</div>

Can u please share me a sample script for my output. I have done one script field for Addition . But, not similar to this use case..

def total = doc['bytesrcvd'].value + doc ['bytessent'].value; return total;

Thanks,  
Vishal

---

<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:** [November 1, 2019, 1:18pm UTC](https://discuss.elastic.co/t/combining-two-documents-within-the-same-index/201857/4 "2019-11-01T13:18:18Z")

</div>

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