# Logstash Syntax :: Force a Newly-Created Field to be an Integer?

**URL:** https://discuss.elastic.co/t/logstash-syntax-force-a-newly-created-field-to-be-an-integer/208562
**Category:** Logstash
**Created:** [November 19, 2019, 6:52pm UTC](https://discuss.elastic.co/t/logstash-syntax-force-a-newly-created-field-to-be-an-integer/208562 "2019-11-19T18:52:54Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![redapplesonly](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/redapplesonly/32/57700_2.png) [@redapplesonly](https://discuss.elastic.co/u/redapplesonly)
#### Post date: [November 19, 2019, 6:52pm UTC](https://discuss.elastic.co/t/logstash-syntax-force-a-newly-created-field-to-be-an-integer/208562/1 "2019-11-19T18:52:54Z")

</div>

Hi Logstash Gurus,

I need to build off another syntax question I asked, [here](https://discuss.elastic.co/t/logstash-syntax-add-a-new-field-which-is-the-product-of-other-fields/208369). In that post, I learned how to use a Ruby Filter to create a new field, then populate that field with the product of other fields:

```
  ruby {
    code => '
      event.set( "[FieldC]", (event.get("[FieldA]").to_i * event.get("[FieldB]").to_i * 5) )
    '
  }

```

This looked correct when I inspected the raw data. But later, I realized that `FieldC` is a string. This is a problem, as I need it to be an integer.

I tried the stupid solution by doing this:

```
  ruby {
    ...same solution from above...
  }
  mutate {
    convert => { "[FieldC]" => "integer" }
  }

```

And then this:

```
  ruby {
    code => '
      event.set( "[FieldC]", (event.get("[FieldA]").to_i * event.get("[FieldB]").to_i * 5).to_i )
    '
  }

```

(I added an “`.to_i`” at the end of the big math section)

But neither of these had an effect. (The [online documentation](https://www.elastic.co/guide/en/logstash/current/event-api.html) also says “_Mutating a collection after setting it in the Event has an undefined behaviour and is not allowed,_” so I guess this was never going to work.)

A careful read of the Event API page ([here](https://www.elastic.co/guide/en/logstash/current/event-api.html)) says:

```
Syntax: event.get(field)

Returns: Value for this field or nil if the field does not exist. Returned values
could be a string, numeric or timestamp scalar value.

```

But I don’t understand how you tell Logstash that `FieldC` is supposed to be an integer. Is there a way to force this? Thank you!

---

<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: [November 19, 2019, 6:55pm UTC](https://discuss.elastic.co/t/logstash-syntax-force-a-newly-created-field-to-be-an-integer/208562/2 "2019-11-19T18:55:57Z")

</div>

> [@redapplesonly](#):
>
> mutate { convert =\> { "[FieldC]" =\> "integer" } }

event.get may return integer, but event.set will never create one. That mutate+convert you tried is the right way to do it.

---

<div class="post-metadata">

### Author: ![redapplesonly](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/redapplesonly/32/57700_2.png) [@redapplesonly](https://discuss.elastic.co/u/redapplesonly)
#### Post date: [November 19, 2019, 8:07pm UTC](https://discuss.elastic.co/t/logstash-syntax-force-a-newly-created-field-to-be-an-integer/208562/3 "2019-11-19T20:07:45Z")

</div>

Thanks Badger,

So I tried to mutate `FieldC` into an integer, but the field remained as a string. But then when I created a new field from scratch:

```auto
ruby {
    code => '
      event.set( "[FieldC2]", (event.get("[FieldA]").to_i * event.get("[FieldB]").to_i * 5) )
    '
  }
  mutate {
    convert => { "[FieldC2]" => "integer" }
  }

```

_ **This** _ worked like a charm. So, all's well that ends well...

Thank you!

PS - Forgot to add, for anyone following this post... I am on Logstash 7.4.0, the Docker Container version

---

<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: [November 19, 2019, 9:04pm UTC](https://discuss.elastic.co/t/logstash-syntax-force-a-newly-created-field-to-be-an-integer/208562/4 "2019-11-19T21:04:38Z")

</div>

If you are talking about the type in elasticsearch, then once you index a field on a document as a string, it will be a string for every other document in that index, even if logstash sends an integer. If you rolled over to a new day's index then it would start getting indexed as an integer even with the old code.

---

<div class="post-metadata">

### Author: ![redapplesonly](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/redapplesonly/32/57700_2.png) [@redapplesonly](https://discuss.elastic.co/u/redapplesonly)
#### Post date: [November 20, 2019, 1:32pm UTC](https://discuss.elastic.co/t/logstash-syntax-force-a-newly-created-field-to-be-an-integer/208562/5 "2019-11-20T13:32:41Z")

</div>

Ohhhhh, you're right. I was changing the string to an integer in Logstash, then checking the data type in Kibana. Kibana, of course, was reporting on what it saw in Elasticsearch.

I'll have to research how to change the data type in ES... but for the moment, my workaround is just fine.

Many thanks!

---

<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: [December 18, 2019, 1:32pm UTC](https://discuss.elastic.co/t/logstash-syntax-force-a-newly-created-field-to-be-an-integer/208562/6 "2019-12-18T13:32:52Z")

</div>

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