# Logstash index naming pattern - use of an alternate tag if one is missing

**URL:** <https://discuss.elastic.co/t/logstash-index-naming-pattern-use-of-an-alternate-tag-if-one-is-missing/215767>\
**Category:** Logstash\
**Created:** [January 20, 2020, 3:42pm UTC](https://discuss.elastic.co/t/logstash-index-naming-pattern-use-of-an-alternate-tag-if-one-is-missing/215767 "2020-01-20T15:42:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jreid](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@jreid](https://discuss.elastic.co/u/jreid)\
**Post date:** [January 20, 2020, 3:42pm UTC](https://discuss.elastic.co/t/logstash-index-naming-pattern-use-of-an-alternate-tag-if-one-is-missing/215767/1 "2020-01-20T15:42:57Z")

</div>

Data is sourced from filebeat and packetbeat with add\_kubernetes\_metadata and passed to logstash, using the logstash helm chart.

In logstash, I want the logs to be output to an elasticsearch index, named based on a kubernetes metadata tag ({[kubernetes][labels][app]}) but there have been a few deployments in kubernetes that were not tagged with app, causing a large amount of data to pile up in indices named with a literal "[kubernetes][labels][app]" (e.g. "logstash-production\_us-east-1-00\_filebeat-%{[kubernetes][labels][app]}-2020.01.20" instead of "logstash-production\_us-east-1-00\_filebeat-someapp-2020.01.20") instead of the correct tag - in these cases, where this metadata field is missing, I would like to use another field instead (e.g. {[kubernetes][labels][name]})

Logstash config:

```
outputs:
  main: |-
    output {
      elasticsearch {
        hosts => ["${ELASTICSEARCH_HOST}:${ELASTICSEARCH_PORT}"]
        manage_template => false
        #index => "logstash-production_us-east-1-00_%{[@metadata][beat]}-%{+YYYY.MM.dd}"
        index => "logstash-production_us-east-1-00_%{[@metadata][beat]}-%{[kubernetes][labels][app]}-%{+YYYY.MM.dd}"
        #document_type => "%{[@metadata][type]}"
        document_type => "_doc"
      }
    }
```

---

<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:** [January 21, 2020, 11:15am UTC](https://discuss.elastic.co/t/logstash-index-naming-pattern-use-of-an-alternate-tag-if-one-is-missing/215767/2 "2020-01-21T11:15:43Z")

</div>

For the config of yours you can do the check:

```auto
if [kubernetes][labels][name]{
      elasticsearch {
        hosts => ["${ELASTICSEARCH_HOST}:${ELASTICSEARCH_PORT}"]
        manage_template => false
        #index => "logstash-production_us-east-1-00_%{[@metadata][beat]}-%{+YYYY.MM.dd}"
        index => "logstash-production_us-east-1-00_%{[@metadata][beat]}-%{[kubernetes][labels][app]}-%{+YYYY.MM.dd}"
        #document_type => "%{[@metadata][type]}"
        document_type => "_doc"
      }
}
else {
      elasticsearch {
        hosts => ["${ELASTICSEARCH_HOST}:${ELASTICSEARCH_PORT}"]
        manage_template => false
        index => "logstash-production_us-east-1-00_%{[@metadata][beat]}-%{+YYYY.MM.dd}"
        #document_type => "%{[@metadata][type]}"
        document_type => "_doc"
      }
}

```

Would you consider to run ILM? Looks more logical for your use case.  
For 7.X ES version you dont have to use document\_type anymore.

---

<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:** [February 18, 2020, 11:15am UTC](https://discuss.elastic.co/t/logstash-index-naming-pattern-use-of-an-alternate-tag-if-one-is-missing/215767/3 "2020-02-18T11:15:44Z")

</div>

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