1
votes

I would like to update all documents inside the the index. I found that Update Query is the method we should use. But, I am facing problem when I am using ctx._now as a value for updated document's field, causing the field value become null.

This is the sample:

{
  "script": {
    "inline": "ctx._source.timenow = ctx._now"
  },
  "query": {
    "match_all": {}
  }
}

timenow evidence

When I am using random number value, it is work. Let say that I put timenow = 5. Then, All documents field timenow become 5. But, it is not work using this ctx method.

How should I do that ?

Additional Information

This is my ES information:

"version" : {
    "number" : "5.5.1",
    "build_hash" : "19c13d0",
    "build_date" : "2017-07-18T20:44:24.823Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.0"
  }

I am using ctx._now because it is work on document update. Here are the case:

{
  "script": "ctx._source.timenow2 = ctx._now"
}

Example: POST index/type/24/_update

Here is the docs from elastic:

In addition to _source, the following variables are available through the ctx map: _index, _type, _id, _version, _routing and _now (the current timestamp).

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html

Thank You

1
where did you see ctx._now? What version of ES are you using?Val
If all of your timenows are being updated to null, that means _now is not a field on ctx. What made you use _now (where you find information about it)? Try setting the timenow values to be Instant.now() or something like that to see how that works.ryanlutgen
I have update my question adding ctx._now. How can I use Instant.now() ?Adityo Setyonugroho

1 Answers

2
votes

_now is only available in the Update API, i.e. when you call the _update endpoint, not _update_by_query.

Use this instead:

{
  "script": {
    "inline": "ctx._source.timenow = Instant.now().toEpochMilli()"
  },
  "query": {
    "match_all": {}
  }
}