2

Please suggest how to remove array field if empty? I have tried below config :

if [tags] == [] {
mutate {
remove_field => ["tags"]
}
}

But getting logstash exception:
[2018-03-01T15:07:56,000][FATAL][logstash.runner ] The given configuration is invalid. Reason: Failed to parse right-hand side of conditional [str]pipeline:31:8:[tags] == []
3
  • i need this in logstash. link suggests to do in elasticsearch. pls suggest Commented Mar 1, 2018 at 10:24
  • I think you'll have to use the ruby filter, I haven't seen a way to do it otherwise. Commented Mar 1, 2018 at 13:48
  • with ruby code i am not able to check if array is empty and delete it. if found a method empty? in ruby but not working with logstash ruby filter. pls help with ruby code snippet any.] Commented Mar 1, 2018 at 13:56

3 Answers 3

4

I didn't know how to remove a field from inside the ruby filter, so when the tags array is empty, I'm setting another field and using to know when to remove the field tags.

ruby {
    code => "event.set('[is_tags_empty]', 'true') if event.get('[tags]').length == 0"
}

if [is_tags_empty] {
    mutate {
        remove_field => ["tags","is_tags_empty"]
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

it worked..! thanks alot. :) whats event.set event.get over here ? i think its pure ruby program wrapped with double quotes inside code block of ruby filter. am i correct?
This event.set('[is_tags_empty]', 'true') if event.get('[tags]').length == 0 is the ruby program. event.get('[tags]') retrieve the field tags from the event, event.set('[is_tags_empty]', 'true') set the field is_tags_empty in the event to the value true.
For more information about the event, see elastic.co/guide/en/logstash/current/event-api.html
1

Check for not null and if array is empty

if [tags] and !([tags][0]) {
    mutate {
        remove_field => ["tags"]
    }
}

Tested on Logstash 7.1

Comments

0

This one worked for me:

ruby {
  code => "if event.get('tags').length == 0 then event.remove('tags') end"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.