# Painless - how to access "@timestamp" field in script

**URL:** <https://discuss.elastic.co/t/painless-how-to-access-timestamp-field-in-script/130263>\
**Category:** Elasticsearch\
**Created:** [May 2, 2018, 2:22pm UTC](https://discuss.elastic.co/t/painless-how-to-access-timestamp-field-in-script/130263 "2018-05-02T14:22:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SKumarMN](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/skumarmn/32/27537_2.png) [@SKumarMN](https://discuss.elastic.co/u/SKumarMN)\
**Post date:** [May 2, 2018, 2:22pm UTC](https://discuss.elastic.co/t/painless-how-to-access-timestamp-field-in-script/130263/1 "2018-05-02T14:22:41Z")

</div>

Hi All,

I have an index with a mapping for the field  
"@timestamp": {  
"format": "epoch\_millis",  
"type": "date"  
}

Using painless i want to update it. However when i try to access this field in the script it throws an error. How to access this field in the script

> POST index/doc/\_update\_by\_query  
> script{  
> "lang":"painless",  
> "inline": "ctx.\_source.@timestamp = ctx.\_source.somedatefield"  
> }

---

<div class="post-metadata">

**Author:** ![shanec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shanec/32/4004_2.png) [@shanec](https://discuss.elastic.co/u/shanec)\
**Post date:** [May 2, 2018, 9:29pm UTC](https://discuss.elastic.co/t/painless-how-to-access-timestamp-field-in-script/130263/2 "2018-05-02T21:29:12Z")

</div>

> [@SKumarMN](#):
>
> POST index/doc/\_update\_by\_query
> 
> script{
> 
> "lang":"painless",
> 
> "inline": "ctx.\_source.@timestamp = ctx.\_source.somedatefield"
> 
> }

You can do `ctx._source['@timestamp'] = ctx._source['somedatefield']`

---

<div class="post-metadata">

**Author:** ![SKumarMN](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/skumarmn/32/27537_2.png) [@SKumarMN](https://discuss.elastic.co/u/SKumarMN)\
**Post date:** [May 3, 2018, 11:55am UTC](https://discuss.elastic.co/t/painless-how-to-access-timestamp-field-in-script/130263/3 "2018-05-03T11:55:16Z")

</div>

@shanec thanks.

How would this be different as shown [https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html)

```
    "lang": "expression",
    "source": "doc['@timestamp']" = doc['somedatefield']
```

---

<div class="post-metadata">

**Author:** ![shanec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shanec/32/4004_2.png) [@shanec](https://discuss.elastic.co/u/shanec)\
**Post date:** [May 3, 2018, 2:04pm UTC](https://discuss.elastic.co/t/painless-how-to-access-timestamp-field-in-script/130263/4 "2018-05-03T14:04:00Z")

</div>

> [@SKumarMN](#):
>
> [How to write scripts | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html)

Note that you/the docs have `"lang": "expression"` here, which is a different language than Painless, but for what I'm going to say next, you can safely ignore this.

There are different contexts in Elasticsearch that scripts can run in, which means that depending on where you run the script, you may have access to different stuff. As a concrete example, [painless scripts in an ingest pipeline](https://www.elastic.co/guide/en/elasticsearch/reference/master/script-processor.html) haven't _yet_ indexed the document, which means they haven't placed in the doc\_values structure we use and it wouldn't make any sense to have a "score" since there's no query going on at that time. But those are 2 things that do make a lot of sense to access from a script at query time. In contrast, there are things that do make sense to access at ingest/update/reindex time which should be avoided at query time -- things like accessing `_source`.

Documentation for this is at [Accessing document fields and special variables | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html), but we definitely could improve the clarity here.

---

<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:** [May 31, 2018, 2:04pm UTC](https://discuss.elastic.co/t/painless-how-to-access-timestamp-field-in-script/130263/5 "2018-05-31T14:04:02Z")

</div>

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