# Syntax for adding tags from couchdb doc array attribute

**URL:** https://discuss.elastic.co/t/syntax-for-adding-tags-from-couchdb-doc-array-attribute/45619
**Category:** Logstash
**Created:** [March 28, 2016, 6:35pm UTC](https://discuss.elastic.co/t/syntax-for-adding-tags-from-couchdb-doc-array-attribute/45619 "2016-03-28T18:35:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![pagameba](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pagameba/32/8724_2.png) [@pagameba](https://discuss.elastic.co/u/pagameba)
#### Post date: [March 28, 2016, 6:35pm UTC](https://discuss.elastic.co/t/syntax-for-adding-tags-from-couchdb-doc-array-attribute/45619/1 "2016-03-28T18:35:12Z")

</div>

Hi, I'm trying to figure out the logstash filter syntax for mutate to add tags (using add\_tag) to an event coming from a couchdb\_changes feed where the tags need to come from a document attribute that is, itself, an array (coincidentally called tags).

The logstash configuration file looks something like this (edited for brevity and security)

```
input {
  couchdb_changes {
    host => "couch"
    port => "5984"
    db => "test"
    codec => "json"
  }
}

filter {

  #some document attributes are turned into event attributes
  mutate {
    add_field => { "doc_id" => "%{[@metadata][_id]}" }
    add_field => { "title" => "%{[doc][name]}" }
    add_field => { "description" => "%{[doc][description]}" }
  }

  # several fields are used to add tags like this
  if [doc][somekey] {
    mutate { add_tag => ["somekey"] }
  }
  
  # How to add all the values in the doc.tags array as tags to the event?

  # we don't want to store the whole doc in elasticsearch because its big.
  mutate { remove_field => ["doc"] }

}

output {
  stdout { codec => "rubydebug" }
  elasticsearch {
    hosts => "elasticsearch:9200"
    document_id => "%{[@metadata][_id]}"
    document_type => "%{[@metadata][type]}"
  }
}

```

As noted, the couchdb document has an attribute called 'tags' which is an array of string values. I would like to add each of these values to the event using add\_tag but I cannot figure out the syntax for doing so.

I have tried something like this:

```
 mutate {
   add_field => { "tags" => "%{[doc][tags]}" }
   split => { "tags" => "," }
 }

```

before using `add_tag` elsewhere but if tags is an empty array or has just one element, then `tags` in the event becomes a string and other values get string-concatenated to it.

I also tried

```
mutate {
  add_tag => ["%{[doc][tags]}" ]
}

```

which adds all the tags as a single comma-separated entry in the tags array, after which I tried using `split` but that didn't seem to fix it.

Any help on syntax here would be appreciated.

---

<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: [March 29, 2016, 5:41am UTC](https://discuss.elastic.co/t/syntax-for-adding-tags-from-couchdb-doc-array-attribute/45619/2 "2016-03-29T05:41:21Z")

</div>

I suspect you need to use a ruby filter for this.

```auto
filter {
  ruby {
    code => "
      event['doc']['tags'].each { |t| event.tag(t) }
    "
  }
}

```

---

<div class="post-metadata">

### Author: ![pagameba](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pagameba/32/8724_2.png) [@pagameba](https://discuss.elastic.co/u/pagameba)
#### Post date: [March 29, 2016, 2:09pm UTC](https://discuss.elastic.co/t/syntax-for-adding-tags-from-couchdb-doc-array-attribute/45619/3 "2016-03-29T14:09:08Z")

</div>

This worked perfectly, thank you sooo much!

---

<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 6, 2017, 5:04am UTC](https://discuss.elastic.co/t/syntax-for-adding-tags-from-couchdb-doc-array-attribute/45619/4 "2017-07-06T05:04:55Z")

</div>


