# \[SOLVED\] Increment field value in logstash filter

**URL:** <https://discuss.elastic.co/t/solved-increment-field-value-in-logstash-filter/166757>\
**Category:** Logstash\
**Created:** [February 1, 2019, 3:53pm UTC](https://discuss.elastic.co/t/solved-increment-field-value-in-logstash-filter/166757 "2019-02-01T15:53:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pastechecker](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@pastechecker](https://discuss.elastic.co/u/pastechecker)\
**Post date:** [February 1, 2019, 3:53pm UTC](https://discuss.elastic.co/t/solved-increment-field-value-in-logstash-filter/166757/1 "2019-02-01T15:53:53Z")

</div>

Hello,  
I have a data taken from the filter elasticsearch query.

> elasticsearch {  
> hosts =\> ["localhost:9200"]  
> index =\> "elasticinput"  
> query =\> '\_id=%{fingerprint}'  
> fields =\> { "request\_ip\_count" =\> "new\_request\_ip\_count" }  
> fields =\> { "request\_counter\_attempts" =\> "new\_request\_counter\_attempts" }  
> fields =\> { "request\_domain\_root" =\> "new\_request\_domain\_root" }  
> }

The fields contains a number:  
"request\_ip\_count":"3"

How can I increment this field by 1?  
I want it to be: "request\_ip\_count":"4"

Is there a way?

---

<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:** [February 1, 2019, 7:13pm UTC](https://discuss.elastic.co/t/solved-increment-field-value-in-logstash-filter/166757/2 "2019-02-01T19:13:36Z")

</div>

How about

```
    ruby {
        code => '
            c = event.get("request_ip_count")
            if c
                event.set("request_ip_count", c.to_i + 1)
            end
        '
    }
```

---

<div class="post-metadata">

**Author:** ![pastechecker](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@pastechecker](https://discuss.elastic.co/u/pastechecker)\
**Post date:** [February 4, 2019, 9:42am UTC](https://discuss.elastic.co/t/solved-increment-field-value-in-logstash-filter/166757/3 "2019-02-04T09:42:14Z")

</div>

Hey Badger,  
my solution last week that I forgot to publish was:

if [request\_ip\_count] != "" {  
ruby {  
code =\> '  
event.set("request\_ip\_count", event.get("request\_ip\_count").to\_i + 1)  
'  
}  
}

---

<div class="post-metadata">

**Author:** ![pastechecker](https://avatars.discourse-cdn.com/v4/letter/p/0ea827/32.png) [@pastechecker](https://discuss.elastic.co/u/pastechecker)\
**Post date:** [February 4, 2019, 9:42am UTC](https://discuss.elastic.co/t/solved-increment-field-value-in-logstash-filter/166757/4 "2019-02-04T09:42:29Z")

</div>

Thank you for your help as well!

---

<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:** [March 4, 2019, 9:42am UTC](https://discuss.elastic.co/t/solved-increment-field-value-in-logstash-filter/166757/5 "2019-03-04T09:42:32Z")

</div>

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