title: How to reuse added field in output with logstash
body: <p>My use case is simple.
  I have a Kafka in input, and some indexes in Elasticsearch (topic name === index name), and where indexes names are the same as the entities we use in our application, like &quot;buildings&quot;, &quot;cars&quot;, &quot;bus&quot; (just for example).</p>
  <pre><code>input {
      kafka {
          bootstrap_servers =&gt; &quot;kafka:29092&quot;
          topics =&gt; ['cars', 'bus']
          decorate_events =&gt; true
          codec =&gt; 'json'
      }
  }
  
  filter {
      if [@metadata][kafka][topic] == 'cars' {
          mutate {
              convert =&gt; {
                  &quot;car_id&quot; =&gt; &quot;integer&quot;
              }
              add_field =&gt; {
                  'id' =&gt; &quot;%{car_id}&quot;
              }
          }
      }
  
      if [@metadata][kafka][topic] == 'bus' {
          mutate {
              convert =&gt; {
                  &quot;bus_id&quot; =&gt; &quot;integer&quot;
              }
              add_field =&gt; {
                  'id' =&gt; &quot;%{bus_id}&quot;
              }
          }
      }
  }
  
  if [@metadata][kafka][topic] == 'cars' {
      elasticsearch {
          hosts =&gt; &quot;elasticsearch:9200&quot;
          user =&gt; &quot;${ELASTICSEARCH_USERNAME}&quot;
          password =&gt; &quot;${ELASTICSEARCH_PASSWORD}&quot;
          index =&gt; &quot;%{[@metadata][kafka][topic]}&quot;
          doc_as_upsert =&gt; true
          action =&gt; 'update'
          document_id =&gt; '%{car_id}'
      }
  
      if '%{[isDelete]}' {
          elasticsearch {
              hosts =&gt; &quot;elasticsearch:9200&quot;
              user =&gt; &quot;${ELASTICSEARCH_USERNAME}&quot;
              password =&gt; &quot;${ELASTICSEARCH_PASSWORD}&quot;
              index =&gt; &quot;%{[@metadata][kafka][topic]}&quot;
              action =&gt; 'delete'
              document_id =&gt; '%{car_id}'
          }
      }
  }
  
  if [@metadata][kafka][topic] == 'cars' {
      elasticsearch {
          hosts =&gt; &quot;elasticsearch:9200&quot;
          user =&gt; &quot;${ELASTICSEARCH_USERNAME}&quot;
          password =&gt; &quot;${ELASTICSEARCH_PASSWORD}&quot;
          index =&gt; &quot;%{[@metadata][kafka][topic]}&quot;
          doc_as_upsert =&gt; true
          action =&gt; 'update'
          document_id =&gt; '%{bus_id}'
      }
  
      if '%{[isDelete]}' {
          elasticsearch {
              hosts =&gt; &quot;elasticsearch:9200&quot;
              user =&gt; &quot;${ELASTICSEARCH_USERNAME}&quot;
              password =&gt; &quot;${ELASTICSEARCH_PASSWORD}&quot;
              index =&gt; &quot;%{[@metadata][kafka][topic]}&quot;
              action =&gt; 'delete'
              document_id =&gt; '%{bus_id}'
          }
      }
  }
  </code></pre>
  <p>To update / delete documents from Logstash, I need to use their id. But you'll understand, I don't want to have 50 conditions for each entities, i prefer factorize.</p>
  <p>I would like to <strong>re-use</strong> the <strong>&quot;id&quot;</strong> I've created in the <strong>filter part</strong>, in the output to use it in <strong>document_id</strong>.</p>
  <p>Do you have any idea about how I could do it ?</p>