# Output to different elastic search Indices based on the string

**URL:** <https://discuss.elastic.co/t/output-to-different-elastic-search-indices-based-on-the-string/291568>\
**Category:** Logstash\
**Created:** [December 12, 2021, 11:53pm UTC](https://discuss.elastic.co/t/output-to-different-elastic-search-indices-based-on-the-string/291568 "2021-12-12T23:53:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mussa572](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@mussa572](https://discuss.elastic.co/u/mussa572)\
**Post date:** [December 12, 2021, 11:53pm UTC](https://discuss.elastic.co/t/output-to-different-elastic-search-indices-based-on-the-string/291568/1 "2021-12-12T23:53:56Z")

</div>

Hi  
I have a Category field (cat) which can have following value and future expect to add more.

BL  
LA  
KA

Currently I have following code to output to the different Elasticsearch indices from our Logstash pipeline . I was wondering is there a dynamic way where I don't have to add a new if statement each time I have a new Category and If I can achieve the following with more dynamic and small code and should automatically output to the right index without me add a new statement each time I add a new category.

```auto
output {
 

  if "BL" in [cat] {

   elasticsearch {

		hosts => "elasticsearch:9200"
		index => "Index-BL"
    }
  
  if "LA" in [cat] {

   elasticsearch {

		hosts => "elasticsearch:9200"
		index => "Index-LA"
    }  
  if "KA" in [cat] {

   elasticsearch {

		hosts => "elasticsearch:9200"
		index => "Index-KA"
    }

```

---

<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:** [December 13, 2021, 1:53am UTC](https://discuss.elastic.co/t/output-to-different-elastic-search-indices-based-on-the-string/291568/2 "2021-12-13T01:53:28Z")

</div>

You can use a [sprintf reference](https://www.elastic.co/guide/en/logstash/current/event-dependent-configuration.html#sprintf). In the output you can use

```
index => "Index-%{cat}"

```

If you only want a certain set of indexes created use

```
if [cat] in ["BA", "KA", "LA"]

```

around the output.

---

<div class="post-metadata">

**Author:** ![mussa572](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@mussa572](https://discuss.elastic.co/u/mussa572)\
**Post date:** [December 13, 2021, 2:12am UTC](https://discuss.elastic.co/t/output-to-different-elastic-search-indices-based-on-the-string/291568/3 "2021-12-13T02:12:20Z")

</div>

Thanks for the reply . I will try and test . Does that mean I can remove the if statements and Logstash will send the output to the right index ?

---

<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:** [December 13, 2021, 3:40am UTC](https://discuss.elastic.co/t/output-to-different-elastic-search-indices-based-on-the-string/291568/4 "2021-12-13T03:40:17Z")

</div>

> [@mussa572](#):
>
> Does that mean I can remove the if statements and Logstash will send the output to the right index ?

That's the idea, yes.

---

<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:** [January 10, 2022, 3:41am UTC](https://discuss.elastic.co/t/output-to-different-elastic-search-indices-based-on-the-string/291568/5 "2022-01-10T03:41:08Z")

</div>

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