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


(Prashant Agrawal) #1

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' -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' -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


(David Pilato) #2

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


(Prashant Agrawal) #3

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


(David Pilato) #4

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


(Prashant Agrawal) #5

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

~Prashant
+91-8097606642


(Prashant Agrawal) #6

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--;}}


(David Pilato) #7

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


(system) #8