# How to update date field in elasticsearch if it has "-"

**URL:** <https://discuss.elastic.co/t/how-to-update-date-field-in-elasticsearch-if-it-has/42999>\
**Category:** Elasticsearch\
**Created:** [February 29, 2016, 12:42pm UTC](https://discuss.elastic.co/t/how-to-update-date-field-in-elasticsearch-if-it-has/42999 "2016-02-29T12:42:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Prashant\_Agrawal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prashant_agrawal/32/74982_2.png) [@Prashant\_Agrawal](https://discuss.elastic.co/u/Prashant_Agrawal)\
**Post date:** [February 29, 2016, 12:42pm UTC](https://discuss.elastic.co/t/how-to-update-date-field-in-elasticsearch-if-it-has/42999/1 "2016-02-29T12:42:29Z")

</div>

I have data in my Elastic index as below :

Indexed data as:

```
curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}'

```

Mapping obtained using

> curl -XGET localhost:9200/twitter

`{"twitter":{"aliases":{},"mappings":{"tweet":{"properties":{"message":{"type":"string"},"post_date":{"type":"date","format":"strict_date_optional_time||epoch_millis"},"user":{"type":"string"}}}},"settings":{"index":{"creation_date":"1456739938440","number_of_shards":"5","number_of_replicas":"1","uuid":"DwhS57l1TsKQFyzY23LSiQ","version":{"created":"2020099"}}},"warmers":{}}}`

Now if I am modifying user as below I am able to do the same:

> curl -XPOST '[http://localhost:9200/twitter/tweet/1/\_update](http://localhost:9200/twitter/tweet/1/_update)' -d '{  
> "script" : "ctx.\_source.user=new\_user",  
> "params" : {  
> "new\_user" : "search"  
> }  
> }'

But when I tried modifying the date fields as below it is giving an exception :

> curl -XPOST '[http://localhost:9200/twitter/tweet/1/\_update](http://localhost:9200/twitter/tweet/1/_update)' -d '{  
> "script" : "ctx.\_source.post\_date='new-date'",  
> "params" : {  
> "new-date" : "2016-02-02T16:12:23"  
> }  
> }'

Exception received is :

> {"error":{"root\_cause":[{"type":"remote\_transport\_exception","reason":"[Anomaly][127.0.0.1:9300][indices:data/write/update[s]]"}],"type":"illegal\_argument\_exception","reason":"failed to execute script","caused\_by":{"type":"script\_exception","reason":"Failed to compile inline script [ctx.\_source.post\_date=new-date] using lang [groovy]","caused\_by":{"type":"script\_exception","reason":"failed to compile groovy script","caused\_by":{"type":"multiple\_compilation\_errors\_exception","reason":"startup failed:\ne90a551666b36d90e4fc5b08d04250da5c4d552d: 1: unexpected token: - @ line 1, column 26.\n ctx.\_source.post\_date=new-date\n  
> ^\n\n1 error\n"}}}}

Now can anyone let me know how I can handle the same.

~Prashant

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 29, 2016, 1:45pm UTC](https://discuss.elastic.co/t/how-to-update-date-field-in-elasticsearch-if-it-has/42999/2 "2016-02-29T13:45:03Z")

</div>

can you try to replace `new-date` with `newdate`? And also replace `ctx.source.postdate='new-date'` with `ctx.source.postdate=newdate`

---

<div class="post-metadata">

**Author:** ![Prashant\_Agrawal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prashant_agrawal/32/74982_2.png) [@Prashant\_Agrawal](https://discuss.elastic.co/u/Prashant_Agrawal)\
**Post date:** [February 29, 2016, 2:37pm UTC](https://discuss.elastic.co/t/how-to-update-date-field-in-elasticsearch-if-it-has/42999/3 "2016-02-29T14:37:03Z")

</div>

Hi David,

If I change new-date to newdate then its working.

But as we have indexed all docs with field name having "-" in between so  
how we could achieve that.

~Prashant  
+91-8097606642

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 29, 2016, 2:57pm UTC](https://discuss.elastic.co/t/how-to-update-date-field-in-elasticsearch-if-it-has/42999/4 "2016-02-29T14:57:42Z")

</div>

In your example `new-date` is a script parameter. Not a field.  
Is it different from your real use case?

---

<div class="post-metadata">

**Author:** ![Prashant\_Agrawal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prashant_agrawal/32/74982_2.png) [@Prashant\_Agrawal](https://discuss.elastic.co/u/Prashant_Agrawal)\
**Post date:** [February 29, 2016, 3:15pm UTC](https://discuss.elastic.co/t/how-to-update-date-field-in-elasticsearch-if-it-has/42999/5 "2016-02-29T15:15:16Z")

</div>

In my real use case I have script parameter and field both as "-" in between

~Prashant  
+91-8097606642

---

<div class="post-metadata">

**Author:** ![Prashant\_Agrawal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prashant_agrawal/32/74982_2.png) [@Prashant\_Agrawal](https://discuss.elastic.co/u/Prashant_Agrawal)\
**Post date:** [February 29, 2016, 6:03pm UTC](https://discuss.elastic.co/t/how-to-update-date-field-in-elasticsearch-if-it-has/42999/6 "2016-02-29T18:03:22Z")

</div>

So any suggestion how can I use the same just for real use case :

I have two fields as last-ip as IP , and last-login as date.

Now I am running a script something like:

> for (int i = 0; i \< ctx.\_source.device.size(); i++){if(ctx.\_source.device[i].browser == browser){ctx.\_source.device[i].last-ip = newip;i--;}}

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 1, 2016, 10:04am UTC](https://discuss.elastic.co/t/how-to-update-date-field-in-elasticsearch-if-it-has/42999/7 "2016-03-01T10:04:34Z")

</div>

Unsure as I'm not super familiar with scripting but may be something like: `ctx._source.device[i]['last-ip'].value`

I took this from [https://www.elastic.co/guide/en/elasticsearch/reference/2.2/search-request-script-fields.html](https://www.elastic.co/guide/en/elasticsearch/reference/2.2/search-request-script-fields.html)

---

<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:** [July 5, 2017, 11:12pm UTC](https://discuss.elastic.co/t/how-to-update-date-field-in-elasticsearch-if-it-has/42999/8 "2017-07-05T23:12:36Z")

</div>


