# Create a calculated field or empty if value missing

**URL:** <https://discuss.elastic.co/t/create-a-calculated-field-or-empty-if-value-missing/181908>\
**Category:** Logstash\
**Created:** [May 20, 2019, 10:20pm UTC](https://discuss.elastic.co/t/create-a-calculated-field-or-empty-if-value-missing/181908 "2019-05-20T22:20:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![GitsBdr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gitsbdr/32/58828_2.png) [@GitsBdr](https://discuss.elastic.co/u/GitsBdr)\
**Post date:** [May 20, 2019, 10:20pm UTC](https://discuss.elastic.co/t/create-a-calculated-field-or-empty-if-value-missing/181908/1 "2019-05-20T22:20:22Z")

</div>

Hi here,

I'm struggling trying to find the best way to create a field been calculated by doing the multiplication of two others. So first, I need to test that both fields are not null and then I can do my multiplication. The important thing is at this end, I need to have this field created anyway, populated with a value or null. So far what I have :

if ![coef] or ![int1] { mutate { add\_field =\> { "int1coef" =\> "" } } } else { ruby { code =\> "event.set('int1coef', event.get('int1') \* event.get('coef'))" } }

Which worked fine when int1 and coef not null, but doesn't when int1 is null, int1coef is an empty string in my kibana index and not tagged as null with the "-" ...  
Does someone has a better idea ?

Thanks  
Regards  
Guillaume

---

<div class="post-metadata">

**Author:** ![yaauie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yaauie/32/23363_2.png) [@yaauie](https://discuss.elastic.co/u/yaauie)\
**Post date:** [May 20, 2019, 11:19pm UTC](https://discuss.elastic.co/t/create-a-calculated-field-or-empty-if-value-missing/181908/2 "2019-05-20T23:19:25Z")

</div>

Your filter is explicitly setting it to an empty string when either value is _false-y_ (e.g., not present).

If I needed a "null" value to be set, I would do something like:

```auto
mutate {
  ruby {
    code => "
      int1 = Integer(event.get('int1'))
      coef = Integer(event.get('coef'))
      value = (int1 * coef) unless int1.nil? || coef.nil?
      event.set('int1coef', value)
    "
  }
}

```

If I didn't need a value to be set if one of the inputs were missing, I would do:

```auto
if [int1] and [coef]
mutate {
  ruby {
    code => "
      int1 = Integer(event.get('int1'))
      coef = Integer(event.get('coef'))
      value = (int1 * coef)
      event.set('int1coef', value)
    "
  }
}

```

If the source fields could hold something that had decimal precision, I would use `Float()` instead of `Integer()` to extract the relevant bits.

---

<div class="post-metadata">

**Author:** ![GitsBdr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gitsbdr/32/58828_2.png) [@GitsBdr](https://discuss.elastic.co/u/GitsBdr)\
**Post date:** [May 21, 2019, 9:09am UTC](https://discuss.elastic.co/t/create-a-calculated-field-or-empty-if-value-missing/181908/3 "2019-05-21T09:09:34Z")

</div>

Hi @yaauie and thanks for your very helpful hand

I tried your first solution as I want my field created at the end anyway - null or populated with a float  
After all, I've got this issue :

**Ruby exception occurred: can't convert nil into Float**  
As said before intx can be null (empty value from database) or integer/float

My conf looks like that :

```auto
filter {
    ruby { code => "
           coef = Float(event.get('coef'))
           int1 = Float(event.get('int1'))
           int2 = Float(event.get('int2'))
           int3 = Float(event.get('int3'))
           int4 = Float(event.get('int4'))
           int5 = Float(event.get('int5'))

           myint1 = (int1* coef) unless int1.nil? || coef.nil?
           myint2 = (int2* coef) unless int2.nil? || coef.nil?
           myint3 = (int3* coef) unless int3.nil? || coef.nil?
           myint4 = (int4* coef) unless int4.nil? || coef.nil?
           myint5 = (int5* coef) unless int5.nil? || coef.nil?

           event.set('int1coef', myint1)
           event.set('int2coef', myint2)
           event.set('int3coef', myint3)
           event.set('int4coef', myint4)
           event.set('int5coef', myint5)
        " }
}   

```

I also had to remove your "mutate" at the begging, seemed to be wrong  
Thanks again  
Guillaume

---

<div class="post-metadata">

**Author:** ![GitsBdr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gitsbdr/32/58828_2.png) [@GitsBdr](https://discuss.elastic.co/u/GitsBdr)\
**Post date:** [May 21, 2019, 11:41am UTC](https://discuss.elastic.co/t/create-a-calculated-field-or-empty-if-value-missing/181908/4 "2019-05-21T11:41:49Z")

</div>

I'll go for something like below :

`event.get('coef') == nil || event.get('int1') == nil ? event.set('int1coef', nil) : event.set('int1coef', event.get('coef') * event.get('int1'))`

What do you think about it ?

---

<div class="post-metadata">

**Author:** ![yaauie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yaauie/32/23363_2.png) [@yaauie](https://discuss.elastic.co/u/yaauie)\
**Post date:** [May 21, 2019, 6:29pm UTC](https://discuss.elastic.co/t/create-a-calculated-field-or-empty-if-value-missing/181908/5 "2019-05-21T18:29:36Z")

</div>

You're right; my suggestion was fragile against empty inputs. This should be better, because it only passes the value to `Kernel#Float()` if it is non-null.

```auto
filter {
    ruby { code => "
           coef = event.get('coef')
           int1 = event.get('int1')
           int2 = event.get('int2')
           int3 = event.get('int3')
           int4 = event.get('int4')
           int5 = event.get('int5')

           coef = Float(coef) unless coef.nil?
           myint1 = (Float(int1)* coef) unless int1.nil? || coef.nil?
           myint2 = (Float(int2)* coef) unless int2.nil? || coef.nil?
           myint3 = (Float(int3)* coef) unless int3.nil? || coef.nil?
           myint4 = (Float(int4)* coef) unless int4.nil? || coef.nil?
           myint5 = (Float(int5)* coef) unless int5.nil? || coef.nil?

           event.set('int1coef', myint1)
           event.set('int2coef', myint2)
           event.set('int3coef', myint3)
           event.set('int4coef', myint4)
           event.set('int5coef', myint5)
        " }
} 

```

---

<div class="post-metadata">

**Author:** ![GitsBdr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gitsbdr/32/58828_2.png) [@GitsBdr](https://discuss.elastic.co/u/GitsBdr)\
**Post date:** [May 22, 2019, 8:35am UTC](https://discuss.elastic.co/t/create-a-calculated-field-or-empty-if-value-missing/181908/6 "2019-05-22T08:35:32Z")

</div>

Hi @yaauie, thanks for coming back again

Your solution works fine, thanks. Have you seen mine above too ?

> [@GitsBdr](#):
>
> event.get('coef') == nil || event.get('int1') == nil ? event.set('int1coef', nil) : event.set('int1coef', event.get('coef') \* event.get('int1'))

Was wondering if it can work as well ?

Guillaume

---

<div class="post-metadata">

**Author:** ![yaauie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yaauie/32/23363_2.png) [@yaauie](https://discuss.elastic.co/u/yaauie)\
**Post date:** [June 4, 2019, 4:17pm UTC](https://discuss.elastic.co/t/create-a-calculated-field-or-empty-if-value-missing/181908/7 "2019-06-04T16:17:22Z")

</div>

> [@GitsBdr](#):
>
> event.get('coef') == nil || event.get('int1') == nil ? event.set('int1coef', nil) : event.set('int1coef', event.get('coef') \* event.get('int1'))

In Ruby, testing an object to see if it is nil is typically done with `Object#nil?`, instead of with `Object#==(other)`; your code will likely work, but a more idiomatic version of it would be:

```auto
(event.get('coef').nil? || event.get('int1').nil?) ? event.set('int1coef', nil) : event.set('int1coef', event.get('coef') * event.get('int1'))

```

This could be [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself)'d up a little bit to be:

```auto
event.set('int1coef', (event.get('coef').nil? || event.get('int1').nil?) ? nil : event.get('coef') * event.get('int1'))

```

---

<div class="post-metadata">

**Author:** ![GitsBdr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gitsbdr/32/58828_2.png) [@GitsBdr](https://discuss.elastic.co/u/GitsBdr)\
**Post date:** [June 6, 2019, 8:38am UTC](https://discuss.elastic.co/t/create-a-calculated-field-or-empty-if-value-missing/181908/8 "2019-06-06T08:38:26Z")

</div>

Oh the last one looks/works perfect ! Thanks a lot  
Have a great day  
Can close it

---

<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:** [July 4, 2019, 8:41am UTC](https://discuss.elastic.co/t/create-a-calculated-field-or-empty-if-value-missing/181908/9 "2019-07-04T08:41:19Z")

</div>

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