# Accessing Event Metadata inside of Elasticsearch Output Script

**URL:** <https://discuss.elastic.co/t/accessing-event-metadata-inside-of-elasticsearch-output-script/132535>\
**Category:** Logstash\
**Created:** [May 18, 2018, 7:44pm UTC](https://discuss.elastic.co/t/accessing-event-metadata-inside-of-elasticsearch-output-script/132535 "2018-05-18T19:44:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hacker\_21](https://avatars.discourse-cdn.com/v4/letter/h/a6a055/32.png) [@hacker\_21](https://discuss.elastic.co/u/hacker_21)\
**Post date:** [May 18, 2018, 7:44pm UTC](https://discuss.elastic.co/t/accessing-event-metadata-inside-of-elasticsearch-output-script/132535/1 "2018-05-18T19:44:30Z")

</div>

Using the elasticsearch output module, I have `doc_as_upsert` set to `true` for many update actions. I'm using the @metadata capability to store the fields which I don't want upserting into ES.

However, I'm having trouble accessing fields inside of @metadata inside the Elasticsearch script function. The below script makes sure the `urls` array is less than 1,001 and also makes sure the new URL being added is unique to the array:

```
output {
elasticsearch{
    hosts => " ***************"
    user => " ****"
    index => " ****"
    password => " *********"
    document_type => "document"
    document_id => "%{[@metadata][domain]}"
    action => "update"
    script => 'if(ctx._source.urls.length < 1001){ boolean match = false; for (url in ctx._source.urls){if (url == params.event.get("[@metadata][url]")){match = true;}} if(match==false){ctx._source.urls.add(params.event.get("[@metadata][url]")); ;}}'
    doc_as_upsert => true
}

```

}

Inside of ES, the URL value gets appended simply as NULL. I viewed the url metadata field inside of rubydebug and it is for sure being added. I can't reference the metadata directly `"%{[@metadata][domain]}"` because I run into script compilation errors for too many generated scripts.

Is there any way to access the metadata.url field inside of the script function for elasticsearch output plugin?

Thank you!

---

<div class="post-metadata">

**Author:** ![hacker\_21](https://avatars.discourse-cdn.com/v4/letter/h/a6a055/32.png) [@hacker\_21](https://discuss.elastic.co/u/hacker_21)\
**Post date:** [May 21, 2018, 6:35pm UTC](https://discuss.elastic.co/t/accessing-event-metadata-inside-of-elasticsearch-output-script/132535/2 "2018-05-21T18:35:05Z")

</div>

_bump_ any ideas?

---

<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:** [June 18, 2018, 6:35pm UTC](https://discuss.elastic.co/t/accessing-event-metadata-inside-of-elasticsearch-output-script/132535/3 "2018-06-18T18:35:06Z")

</div>

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