# RUBY Filter to event.set each entity in an Array

**URL:** <https://discuss.elastic.co/t/ruby-filter-to-event-set-each-entity-in-an-array/277749>\
**Category:** Logstash\
**Created:** [July 5, 2021, 12:18am UTC](https://discuss.elastic.co/t/ruby-filter-to-event-set-each-entity-in-an-array/277749 "2021-07-05T00:18:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dallas\_Toth](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dallas_toth/32/22630_2.png) [@Dallas\_Toth](https://discuss.elastic.co/u/Dallas_Toth)\
**Post date:** [July 5, 2021, 12:18am UTC](https://discuss.elastic.co/t/ruby-filter-to-event-set-each-entity-in-an-array/277749/1 "2021-07-05T00:18:18Z")

</div>

I am stuck on how to use a Ruby script to manipulate a field with in an array. If anyone can help with this I need to take the Field "[body][entities][sentiment][confidence]" and \* 100 and add that as another Field "[body][entities][sentiment][probability]" for each entity  
Example

```auto
if [body][entities][sentiment][label] == "pos"
  {
ruby { code => 'event.set("[body][entities][sentiment][probability]" , (event.get("[body][entities][sentiment][confidence]").to_f * 100)).to_f' }
  }

```

Here is the Single JSON Document.

```auto
{
  "_index": "logstash-2021.02",
  "_type": "_doc",
  "_id": "1111",
  "_score": 1,
  "_source": {
    "body": {
      "entities": [
        {
          "normalized": "NAME1",
          "sentiment": {
            "label": "pos",
            "confidence": 0.5776256
          }
        },
        {
          "normalized": "NAME2",
          "sentiment": {
            "label": "pos",
            "confidence": 0.47355394
          }
        },
        {
          "normalized": "NAME3",
          "sentiment": {
            "label": "pos",
            "confidence": 0.57258118
          }
        },
        {
          "normalized": "NAME4",
          "sentiment": {
            "label": "pos",
            "confidence": 0.61671584
          }
        }
      ]
    },
    "reference": "TEST",
    "@timestamp": "2021-02-05T09:00:11.000Z",
    "tags": [
      "TEST"
    ],
    "@version": "1",
    "type": "document"
  },
  "fields": {
    "@timestamp": [
      "2021-02-05T09:00:11.000Z"
    ]
  }
}

```

---

<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 5, 2021, 1:56am UTC](https://discuss.elastic.co/t/ruby-filter-to-event-set-each-entity-in-an-array/277749/2 "2021-07-05T01:56:55Z")

</div>

I have not tested it but you would have to do something like

```
ruby {
    code => '
        entities = event.get("[body][entities]")
        if entities.is_a? Array
            entities.each_index { |x|
                entities[x]["sentiment"]["probability"] = entities[x]["sentiment"][confidence] * 100
            }
            event.set("[body][entities]", entities)
        end
    '
}
```

---

<div class="post-metadata">

**Author:** ![Dallas\_Toth](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dallas_toth/32/22630_2.png) [@Dallas\_Toth](https://discuss.elastic.co/u/Dallas_Toth)\
**Post date:** [July 21, 2021, 12:42am UTC](https://discuss.elastic.co/t/ruby-filter-to-event-set-each-entity-in-an-array/277749/3 "2021-07-21T00:42:44Z")

</div>

> [@Badger](#):
>
> ```auto
> ruby {
> code => '
> entities = event.get("[body][entities]")
> if entities.is_a? Array
> entities.each_index { |x|
> entities[x]["sentiment"]["probability"] = entities[x]["sentiment"][confidence] * 100
> }
> event.set("[body][entities]", entities)
> end
> '
> }
> 
> ```

It worked just missed some quotes around [confidence] \* 100 should be ["confidence"] \* 100  
Was also able to add some if's in there for different formulas.  
Thanks again for the help.

---

<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 18, 2021, 12:42am UTC](https://discuss.elastic.co/t/ruby-filter-to-event-set-each-entity-in-an-array/277749/4 "2021-08-18T00:42:46Z")

</div>

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