# Use @timestamp in a \_update\_by\_query

**URL:** <https://discuss.elastic.co/t/use-timestamp-in-a-update-by-query/109581>\
**Category:** Elasticsearch\
**Created:** [November 29, 2017, 11:52am UTC](https://discuss.elastic.co/t/use-timestamp-in-a-update-by-query/109581 "2017-11-29T11:52:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frances\_Buontempo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frances_buontempo/32/16050_2.png) [@Frances\_Buontempo](https://discuss.elastic.co/u/Frances_Buontempo)\
**Post date:** [November 29, 2017, 11:52am UTC](https://discuss.elastic.co/t/use-timestamp-in-a-update-by-query/109581/1 "2017-11-29T11:52:18Z")

</div>

I'm trying to add a field to documents in an index.  
I want to bake in the @timestamp field.

If I use something like the following in a curl -XPOST  
-d '{  
"script" : { "ctx.source.extra\_field = ctx.\_source.timestamp;", "lang" : "painless" },  
"query" : " { "match\_all": {} }  
}'

I get an extra field with null in.  
Of course, saying ctx.\_source.@timestamp is a syntax error.

(I'm actually going to try to concatenate a few fields so we can avoid a like/contains lookup, however tried a simple example first, which doesn't work.)

* * *

I have seen [Alerting: Access @timestamp in transform throws Exception](https://discuss.elastic.co/t/alerting-access-timestamp-in-transform-throws-exception/69677) and also tried

`"ctx.source.extra_field = ctx._source.['@timestamp'];"`

This complains illegal argument exception about the '@' characeter.

---

<div class="post-metadata">

**Author:** ![nick.e](https://avatars.discourse-cdn.com/v4/letter/n/8dc957/32.png) [@nick.e](https://discuss.elastic.co/u/nick.e)\
**Post date:** [November 29, 2017, 12:14pm UTC](https://discuss.elastic.co/t/use-timestamp-in-a-update-by-query/109581/2 "2017-11-29T12:14:50Z")

</div>

Try changing `ctx.source.extra_field = ctx._source.['@timestamp'];`

to `ctx.source.extra_field = ctx._source['@timestamp'];"`

The **dot** after ctx.\_source must not be there.

---

<div class="post-metadata">

**Author:** ![Frances\_Buontempo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frances_buontempo/32/16050_2.png) [@Frances\_Buontempo](https://discuss.elastic.co/u/Frances_Buontempo)\
**Post date:** [November 29, 2017, 1:23pm UTC](https://discuss.elastic.co/t/use-timestamp-in-a-update-by-query/109581/3 "2017-11-29T13:23:24Z")

</div>

OK - good spot.

Without the dot it still complains about an "unexpected character [@]"

I suspect it's hsving all the curl in a file actually...if I just put the payload in and send that to curl I might be good.

---

<div class="post-metadata">

**Author:** ![Frances\_Buontempo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frances_buontempo/32/16050_2.png) [@Frances\_Buontempo](https://discuss.elastic.co/u/Frances_Buontempo)\
**Post date:** [November 29, 2017, 1:30pm UTC](https://discuss.elastic.co/t/use-timestamp-in-a-update-by-query/109581/4 "2017-11-29T13:30:36Z")

</div>

And indeed start the curl with`'` in a script meant the next quote round the @timestamp broke things.

Note to self - put the payload in the file and use `--data "@add_field"`with the script in the file.

---

<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:** [December 27, 2017, 1:31pm UTC](https://discuss.elastic.co/t/use-timestamp-in-a-update-by-query/109581/5 "2017-12-27T13:31:07Z")

</div>

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