Elasticsearch Update by Query using CTX

时间:2017-12-18 08:31:07

标签: elasticsearch

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:

QDataStream stream(socket);
ushort MessageId, MessageSize;
stream.startTransaction();
stream >> MessageId >> MessageSize;
if(stream.commitTransaction())
    qDebug() << "Worked:" << MessageId << MessageSize;
else
    qDebug() << "Error:" << stream.status();

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:

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

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

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

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 个答案:

答案 0 :(得分:2)

_now仅在更新API中提供,即当您调用_update端点时,而不是_update_by_query

请改用:

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