# Add or update field in filter or elsewhere

**URL:** <https://discuss.elastic.co/t/add-or-update-field-in-filter-or-elsewhere/106297>\
**Category:** Logstash\
**Created:** [November 3, 2017, 11:04am UTC](https://discuss.elastic.co/t/add-or-update-field-in-filter-or-elsewhere/106297 "2017-11-03T11:04:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ram\_Sharan\_Mittal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ram_sharan_mittal/32/47018_2.png) [@Ram\_Sharan\_Mittal](https://discuss.elastic.co/u/Ram_Sharan_Mittal)\
**Post date:** [November 3, 2017, 11:04am UTC](https://discuss.elastic.co/t/add-or-update-field-in-filter-or-elsewhere/106297/1 "2017-11-03T11:04:03Z")

</div>

based on [@metadata][category] field , log need to uploaded to different file location. if message have some pattern then need to update [@metadata][category] , I am doing in filter

filter {  
if [message] =~ "._regextpattern._"  
{ grok{ add\_field =\> {"[@metadata][category]" =\> "file1"} } }  
else  
{ grok{ add\_field =\> {"[@metadata][category]" =\> "file2"} } }

}

so it go desired output. how to update this field at filter or input. at input [@metadata][category] can be added as field.

I tried with grok but previous value "[@metadata][category]" =\> "file2" and pattern matched and it make "[@metadata][category]" =\> ["file2" , file1"]  
and output plugin not working as desire

if [@metadata][category] == "file1" {  
file{  
path =\> "/tmp/file3.log"  
}  
}else {  
file{  
path =\> "/tmp/file4.log"  
}  
}

then I try with mutate filter with update setting but it need [@metadata][category] to present.

filter {  
if [message] =~ "._regextpattern._"  
{ mutate { update =\> { "[@metadata][category]" =\> "file1" }  
} }

}

Can there be a better approach which sure shot update field means if it not present then add else overwrite it.

I can we do in filter or there is better place. Need to also consider performance.

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [November 6, 2017, 6:14am UTC](https://discuss.elastic.co/t/add-or-update-field-in-filter-or-elsewhere/106297/2 "2017-11-06T06:14:58Z")

</div>

> Can there be a better approach which sure shot update field means if it not present then add else overwrite it.

How about the mutate filter's `replace` option?

---

<div class="post-metadata">

**Author:** ![Ram\_Sharan\_Mittal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ram_sharan_mittal/32/47018_2.png) [@Ram\_Sharan\_Mittal](https://discuss.elastic.co/u/Ram_Sharan_Mittal)\
**Post date:** [November 6, 2017, 7:38am UTC](https://discuss.elastic.co/t/add-or-update-field-in-filter-or-elsewhere/106297/3 "2017-11-06T07:38:20Z")

</div>

that is great... Can do something with grok filter?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [November 6, 2017, 8:22am UTC](https://discuss.elastic.co/t/add-or-update-field-in-filter-or-elsewhere/106297/4 "2017-11-06T08:22:35Z")

</div>

Maybe. If the string you want to store in `[@metadata][category]` can be extracted from another field then grok is a good option.

---

<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 4, 2017, 8:22am UTC](https://discuss.elastic.co/t/add-or-update-field-in-filter-or-elsewhere/106297/5 "2017-12-04T08:22:36Z")

</div>

Closed on December 4, 2017, 8:22am UTC

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