# Logstash Combine two fields of different documents based on another field (without an ID)

**URL:** <https://discuss.elastic.co/t/logstash-combine-two-fields-of-different-documents-based-on-another-field-without-an-id/230461>\
**Category:** Logstash\
**Created:** [April 30, 2020, 5:21am UTC](https://discuss.elastic.co/t/logstash-combine-two-fields-of-different-documents-based-on-another-field-without-an-id/230461 "2020-04-30T05:21:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nani\_20](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@Nani\_20](https://discuss.elastic.co/u/Nani_20)\
**Post date:** [April 30, 2020, 5:21am UTC](https://discuss.elastic.co/t/logstash-combine-two-fields-of-different-documents-based-on-another-field-without-an-id/230461/1 "2020-04-30T05:21:48Z")

</div>

I am using the latest version of logstash(7.6.2). I am trying to add two fields based on values in an another field. Find below my sample data with fields **TimeStamp, Value and Event**. Also, find below my desired output.

**Sample Data**

```auto
18/May/2015:02:05:10 +0000 10 a
18/May/2015:02:05:15 +0000 20 b
18/May/2015:02:05:26 +0000 30 a
18/May/2015:02:05:48 +0000 40 b
18/May/2015:15:05:08 +0000 50 a
18/May/2015:16:05:03 +0000 60 b

```

**Desired Output with extra field**

```auto
18/May/2015:02:05:10 +0000 10 a
18/May/2015:02:05:15 +0000 20 b 30
18/May/2015:02:05:26 +0000 30 a
18/May/2015:02:05:48 +0000 40 b 70
18/May/2015:15:05:08 +0000 50 a
18/May/2015:16:05:03 +0000 60 b 110

```

What I was trying to do is to add the field **Value** based on **Event** and create a new field for **Event=b**.

**Elapsed filter** does something like this but it finds the difference of timestamps. But, what i want is to add **Value** fields based on **Event tags**. I tried using aggregate but it requires a unique ID. Is there any way to do this without using ID or creating dynamic IDS?

Any help here is appreciated. Thanks in advance

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [April 30, 2020, 3:13pm UTC](https://discuss.elastic.co/t/logstash-combine-two-fields-of-different-documents-based-on-another-field-without-an-id/230461/2 "2020-04-30T15:13:57Z")

</div>

You can do that with an aggregate filter. Use mutate+add\_field to add a task\_id (which can be a constant) then if the last field is a then set map\_action =\> "create" and if the last field is b then set map\_action =\> "update" and end\_of\_task =\> true

Make sure you [disable java\_execution](https://github.com/elastic/logstash/issues/10938) in addition to setting pipeline.workers 1 so that the order of events is preserved.

---

<div class="post-metadata">

**Author:** ![Nani\_20](https://avatars.discourse-cdn.com/v4/letter/n/4491bb/32.png) [@Nani\_20](https://discuss.elastic.co/u/Nani_20)\
**Post date:** [May 1, 2020, 5:35am UTC](https://discuss.elastic.co/t/logstash-combine-two-fields-of-different-documents-based-on-another-field-without-an-id/230461/3 "2020-05-01T05:35:12Z")

</div>

> [@Badger](#):
>
> Use mutate+add\_field to add

Thanks for your reply @Badger. I have used a **Ruby class variable** to capture the value between the events. It worked wonderfully. I am sure the aggregate filter you suggested will also do the task. But, I want to understand which one would be efficient in terms of performance? Thanks in advance!

---

<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:** [May 29, 2020, 5:35am UTC](https://discuss.elastic.co/t/logstash-combine-two-fields-of-different-documents-based-on-another-field-without-an-id/230461/4 "2020-05-29T05:35:14Z")

</div>

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