# Logstash: Adding custom field to documennt

**URL:** https://discuss.elastic.co/t/logstash-adding-custom-field-to-documennt/139361
**Category:** Logstash
**Created:** [July 10, 2018, 1:59pm UTC](https://discuss.elastic.co/t/logstash-adding-custom-field-to-documennt/139361 "2018-07-10T13:59:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![delly.fofie](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@delly.fofie](https://discuss.elastic.co/u/delly.fofie)
#### Post date: [July 10, 2018, 1:59pm UTC](https://discuss.elastic.co/t/logstash-adding-custom-field-to-documennt/139361/1 "2018-07-10T13:59:12Z")

</div>

Hi all.  
I have the following XML file format:

```
     <?xml version="1.0" encoding="UTF-8"?>
<Object id="1" MUID="XXXX" GUID="XXXXXX2">
	<Attribute name="Others" value="---"/>
	<Attribute name="Module name" value="Anforderungen_von_AbC_und_AbP_an_ORC"/>
	<Attribute name="fullName" value="xxxxxxxxxxxx"/>
	<Attribute name="ModuleUniqueID" value=""/>
	<Attribute name="Last Modified On" value="04/16/18 16:57:49"/>
	<Attribute name="ModuleType" value="CRS"/>
	<Attribute name="Current Date" value="06/29/18 06:47:55"/>
</Object>
<Object id="1" MUID="XXXX" GUID="XXXXXX3">
	<Attribute name="Others" value="---"/>
	<Attribute name="Module name" value="Anforderungen_von_AbC_und_AbP_an_ORC"/>
	<Attribute name="fullName" value="xxxxxxxxxxxx"/>
	<Attribute name="ModuleUniqueID" value=""/>
	<Attribute name="Last Modified On" value="04/16/18 16:57:49"/>
	<Attribute name="ModuleType" value="CRS"/>
	<Attribute name="Current Date" value="06/29/18 06:47:55"/>
</Object>

```

I have liked to give a custom ID to each of this object while indexing it into elasticsearch. The ID should be a combination of the Last Modified On + Object GUID. I'm not the Logstash expert, so it's why i'm asking here if some one can give me a small tip on how to realise it. I'm pretty sure it can be made with the Ruby plugin or the XML plugin but not sure what exactly to do.

PS: The attributes are very dynamic, the order is not always the same.

Cheers.

---

<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: [July 10, 2018, 2:19pm UTC](https://discuss.elastic.co/t/logstash-adding-custom-field-to-documennt/139361/2 "2018-07-10T14:19:26Z")

</div>

What does the XML look like?

---

<div class="post-metadata">

### Author: ![delly.fofie](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@delly.fofie](https://discuss.elastic.co/u/delly.fofie)
#### Post date: [July 10, 2018, 2:48pm UTC](https://discuss.elastic.co/t/logstash-adding-custom-field-to-documennt/139361/3 "2018-07-10T14:48:33Z")

</div>

I Updated the question. Somehow the XML content was not displayed!

---

<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: [July 10, 2018, 3:49pm UTC](https://discuss.elastic.co/t/logstash-adding-custom-field-to-documennt/139361/4 "2018-07-10T15:49:59Z")

</div>

I would use a multiline codec for that

```
input { stdin { codec => multiline { pattern => "</Object>" negate => true what => "next" auto_flush_interval => 3 } } }

```

Then an xml filter

```
filter { xml { source => "message" store_xml => true target => "theXML" force_array => false } }

```

At that point you have a couple of choices. You could flatten the attributes using a ruby filter.

```
    ruby {
        code => '
            event.get("[theXML][Attribute]").each { |v|
                event.set("[Attributes]" + v["name"], v["value"])
            }
        '
    }

```

Or you could iterate over the set and just stash the value you want in a temporary place, then use it to build the id.

```
    ruby {
        code => '
            event.get("[theXML][Attribute]").each { |v|
                if v["name"] == "Last Modified On"
                    event.set("[@metadata][lastModified]" , v["value"])
                end
            }
        '
    }
```

---

<div class="post-metadata">

### Author: ![delly.fofie](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@delly.fofie](https://discuss.elastic.co/u/delly.fofie)
#### Post date: [July 11, 2018, 6:43am UTC](https://discuss.elastic.co/t/logstash-adding-custom-field-to-documennt/139361/5 "2018-07-11T06:43:17Z")

</div>

> [@Badger](#):
>
> iterate

Thanks for your answer. I will try this. It looks like it's what I was searching for!

---

<div class="post-metadata">

### Author: ![delly.fofie](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@delly.fofie](https://discuss.elastic.co/u/delly.fofie)
#### Post date: [July 11, 2018, 9:21am UTC](https://discuss.elastic.co/t/logstash-adding-custom-field-to-documennt/139361/6 "2018-07-11T09:21:06Z")

</div>

It works like a charm!

---

<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 8, 2018, 9:24am UTC](https://discuss.elastic.co/t/logstash-adding-custom-field-to-documennt/139361/7 "2018-08-08T09:24:21Z")

</div>

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