# What's the proper way to set the op\_type metadata field?

**URL:** <https://discuss.elastic.co/t/whats-the-proper-way-to-set-the-op-type-metadata-field/258323>\
**Category:** Beats\
**Tags:** filebeat\
**Created:** [December 10, 2020, 6:54pm UTC](https://discuss.elastic.co/t/whats-the-proper-way-to-set-the-op-type-metadata-field/258323 "2020-12-10T18:54:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdmcalee](https://avatars.discourse-cdn.com/v4/letter/j/90ced4/32.png) [@jdmcalee](https://discuss.elastic.co/u/jdmcalee)\
**Post date:** [December 10, 2020, 6:54pm UTC](https://discuss.elastic.co/t/whats-the-proper-way-to-set-the-op-type-metadata-field/258323/1 "2020-12-10T18:54:50Z")

</div>

I am trying to have filebeat use the index operation rather than create, and after digging through the beats source code, I came to this [pull request](https://github.com/elastic/beats/pull/12606) where support was added to set the @metadata.op\_type field to index, create, or delete to perform those operations. However, no documentation changes were included in the pull request and no discussion of how to actually use the functionality was either.

After further examining the source code, I determined that only certain processors allow you to correctly add/set metadata fields. If you try to use the add\_fields processor, for example, you'll somehow end up with a second @metadata field in your event. Different processors set field values in different ways, and it seems only the event.PutValue method correctly sets values in event's true @metadata field.

All that said, the best working arrangement I have so far is to:

1. use the add\_fields processor to add some normal field to the event with the desired value of op\_type
2. use the convert processor (it's one that uses event.PutValue) to copy the value from the added field into the @metadata.op\_type field (as type string).
3. use the drop\_fields processor to remove the field added in step 1.

I am hoping to find a more elegant solution, but I can go with that if necessary.

---

<div class="post-metadata">

**Author:** ![jdmcalee](https://avatars.discourse-cdn.com/v4/letter/j/90ced4/32.png) [@jdmcalee](https://discuss.elastic.co/u/jdmcalee)\
**Post date:** [December 28, 2020, 2:36pm UTC](https://discuss.elastic.co/t/whats-the-proper-way-to-set-the-op-type-metadata-field/258323/2 "2020-12-28T14:36:55Z")

</div>

For posterity's sake, thanks to someone opening [this issue](https://github.com/elastic/beats/issues/23091) there is indeed a better solution:

```auto
processors:
  - script:
      lang: javascript
      id: my_filter
      source: >
        function process(event) {
            event.Put("@metadata.op_type", "index");
        }

```

---

<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 25, 2021, 4:37pm UTC](https://discuss.elastic.co/t/whats-the-proper-way-to-set-the-op-type-metadata-field/258323/3 "2021-01-25T16:37:09Z")

</div>

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