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

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