# Ruby Script to rename fields base on a dictionary

**URL:** <https://discuss.elastic.co/t/ruby-script-to-rename-fields-base-on-a-dictionary/303413>\
**Category:** Logstash\
**Created:** [April 27, 2022, 2:02pm UTC](https://discuss.elastic.co/t/ruby-script-to-rename-fields-base-on-a-dictionary/303413 "2022-04-27T14:02:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![frank\_esg](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@frank\_esg](https://discuss.elastic.co/u/frank_esg)\
**Post date:** [April 27, 2022, 2:02pm UTC](https://discuss.elastic.co/t/ruby-script-to-rename-fields-base-on-a-dictionary/303413/1 "2022-04-27T14:02:59Z")

</div>

Hello,  
we use in a logstash pipeline an external ruby script which contains several methods to process logfiles.  
now we want to add a new method to rename all top level fields of a document based on a hashtable.  
I have adapted a method that was used to filter nested fields.

```auto
def filter(event)
    transformKnownFields(event)
    return [event]
end

def transformKnownFields(event)
    events = event.get()
    for k,v in events do
        fieldname = k.downcase
        if fieldname == "elapsed"
            v = v*1000
        end
        ecsfield = $ecsmappings[fieldname]
        event.set(ecsfield, v)
        event.remove(k)
    end
end

$ecsmappings = {
"action" => "[event][action]",
"actionname" => "[event][action]",
"controller" => "[event][category]",
"controllername" => "[event][category]",
"correlationid" => "[trace][id]",
"culture" => "[client][geo][region_iso_code]",
"duration" => "[event][duration]", 
"elapsed" => "[event][duration]",
"elapsedmilliseconds" => "[event][duration]",
"exception" => "[error][type]",
"exceptionmessage" => "[error][message]",
"host" => "[url][domain]",
"httpmethod" => "[http][request][method]",
"level" => "[log][level]", 
"machinename" => "[host][name]"
}

```

Unfortunately it doesn't work like that. i also tried to use "event.to.hash" but i had no luck.

i'm not very familiar with ruby and i'm not really getting anywhere for days now. can someone give me please some advise

regards,  
Frank

---

<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 27, 2022, 2:17pm UTC](https://discuss.elastic.co/t/ruby-script-to-rename-fields-base-on-a-dictionary/303413/2 "2022-04-27T14:17:52Z")

</div>

Try this

```
    ruby {
        init => '
            @ecsmappings = {
                "action" => "[event][action]",
                "actionname" => "[event][action]",
                "controller" => "[event][category]",
                "controllername" => "[event][category]",
                "correlationid" => "[trace][id]",
                "culture" => "[client][geo][region_iso_code]",
                "duration" => "[event][duration]",
                "elapsed" => "[event][duration]",
                "elapsedmilliseconds" => "[event][duration]",
                "exception" => "[error][type]",
                "exceptionmessage" => "[error][message]",
                "host" => "[url][domain]",
                "httpmethod" => "[http][request][method]",
                "level" => "[log][level]",
                "machinename" => "[host][name]"
            }
        '
        code => '
            event.to_hash.each { |k, v|
                fieldname = k.downcase
                if fieldname == "elapsed"
                    v = v*1000
                end
                ecsfield = @ecsmappings[fieldname]
                if ecsfield
                    event.set(ecsfield, v)
                    event.remove(k)
                end
            }
        '
    }
```

---

<div class="post-metadata">

**Author:** ![frank\_esg](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@frank\_esg](https://discuss.elastic.co/u/frank_esg)\
**Post date:** [April 27, 2022, 3:13pm UTC](https://discuss.elastic.co/t/ruby-script-to-rename-fields-base-on-a-dictionary/303413/3 "2022-04-27T15:13:17Z")

</div>

Hy Badger,  
thanks for the quick reply. unfortunately the code doesn't work when i put it in our external ruby script that is called from within logstash pipeline.

i assume that this example only works directly in the pipeline or?  
is it not possible to use the event.to\_hash function in external ruby scripts?

regards,  
Frank

---

<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 27, 2022, 3:38pm UTC](https://discuss.elastic.co/t/ruby-script-to-rename-fields-base-on-a-dictionary/303413/4 "2022-04-27T15:38:03Z")

</div>

You could use the same code in your transformKnownFields method.

---

<div class="post-metadata">

**Author:** ![frank\_esg](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@frank\_esg](https://discuss.elastic.co/u/frank_esg)\
**Post date:** [April 28, 2022, 8:23am UTC](https://discuss.elastic.co/t/ruby-script-to-rename-fields-base-on-a-dictionary/303413/5 "2022-04-28T08:23:27Z")

</div>

Hi,  
thank you. I finally figured it out, there was a small mistake on my side.

regards,  
Frank

---

<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 26, 2022, 8:24am UTC](https://discuss.elastic.co/t/ruby-script-to-rename-fields-base-on-a-dictionary/303413/6 "2022-05-26T08:24:14Z")

</div>

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