How to update a filed with current date and time in ES5.6?

Hi Experts,

I want to update my existing field with current date and time so I am using below srcipt and update by query API to achieve the same but I am not getting the desired result .

POST abc-2017.09/_update_by_query
{
  "script": {
    "inline": "ctx._source.created_on = 'datetime.now()'; ctx._source.number = 'INC5523123';",
    "lang": "painless"
  },
  
  "query": {
    "match": {
      "eventId": "95392012643"
    }
  }
}

When I saw the output I see below

"created_on": "datetime.now()",
"number" : "INC5523123"

It is not filling it as a date . Please help , my field is a text as of now , do I need to change it to date ?

Regards
VG

Rather than using the fairly slow update query just to modify one field why not use a pipeline to set the processing time as the document gets indexed (or updated). Something like

{
    "description" : "Pipeline for setting the document processing time",
    "processors" : [
      {
        "script" : {
          "lang" : "painless",
          "inline" : "DateFormat df = new SimpleDateFormat(\"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'\");\ndf.setTimeZone(TimeZone.getTimeZone(\"UTC\"));\nDate date = new Date();\nctx.proctime = df.format(date);"
        }
      }
    ]
}

Here the field "proctime" is created and set to the current time for each document indexed through this pipeline processor. All you need is to pass along the pipeline=<my_named_pipe> parameter when indexing documents.

1 Like

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