# Store multiple values using ruby in multivalue field

**URL:** <https://discuss.elastic.co/t/store-multiple-values-using-ruby-in-multivalue-field/316059>\
**Category:** Logstash\
**Created:** [October 7, 2022, 10:41am UTC](https://discuss.elastic.co/t/store-multiple-values-using-ruby-in-multivalue-field/316059 "2022-10-07T10:41:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bertr](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@bertr](https://discuss.elastic.co/u/bertr)\
**Post date:** [October 7, 2022, 10:41am UTC](https://discuss.elastic.co/t/store-multiple-values-using-ruby-in-multivalue-field/316059/1 "2022-10-07T10:41:11Z")

</div>

By nature all fields in elasticsearch are multivalue-enabled, i.e. they can have multiple values in one document. Using mutate filter it is easy to add multiple values, but I need to use ruby. I am trying to add multiple values using the logstash ruby even API, but only the last value added is kept, basically overwriting previous values.

Example:

event.set(field, 1);  
event.set(field, 2)

I was hoping that field would contain 1 and 2 as values, but only value 2 is kept. Is there another event API call (append?) that can do this?

---

<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:** [October 7, 2022, 4:19pm UTC](https://discuss.elastic.co/t/store-multiple-values-using-ruby-in-multivalue-field/316059/2 "2022-10-07T16:19:42Z")

</div>

You would need an if-else similar to the [ruby implementation](https://github.com/elastic/logstash/blob/46babd6041eba476ba61abd5c3ce5486dc7f2997/logstash-core/lib/logstash/util/decorators.rb#L29) of mutate+add\_field.

---

<div class="post-metadata">

**Author:** ![bertr](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@bertr](https://discuss.elastic.co/u/bertr)\
**Post date:** [October 8, 2022, 1:40pm UTC](https://discuss.elastic.co/t/store-multiple-values-using-ruby-in-multivalue-field/316059/3 "2022-10-08T13:40:57Z")

</div>

Perfect, thanks!

The following works like a charm:

```auto
arrayfield=[]
arrayfield.push(1)
arrayfield.push(2)
event.set(field, arrayfield)

```

Now field contains two values: 1 and 2

---

<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 5, 2022, 1:41pm UTC](https://discuss.elastic.co/t/store-multiple-values-using-ruby-in-multivalue-field/316059/4 "2022-11-05T13:41:15Z")

</div>

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