[英]Update by query with a clause in ElasticSearch
我有一個elasticsearch指數,其中一些記錄有@timestamp
2月1日的還有在現場_source
稱為process_time
這是與相同每條記錄不同@timestamp
。
{
"_index": "elasticsearch-index",
"_type": "doc",
"_id": "qByop2gBw60PM5VYP0aG",
"_score": 1,
"_source": {
"task": "INFO",
"@timestamp": "2019-02-01T06:04:08.365Z",
"num_of_batches": 0,
"batch_size": 1000,
"process_time": "2019-02-04 06:04:04,489"
}
},
{
"_index": "elasticsearch-index",
"_type": "doc",
"_id": "qByop2gBw60PM5VYP0aG",
"_score": 1,
"_source": {
"task": "INFO",
"@timestamp": "2019-02-01T06:04:08.365Z",
"num_of_batches": 0,
"batch_size": 1000,
"process_time": "2019-02-05 06:04:04,489"
}
}
我想將所有具有@timestamp
作為2月1日的記錄的@timestamp
更新為該記錄中的process_time
。
我怎樣才能做到這一點?
編輯:
在@mysterion的回答之后,我做了如下更改:
{
"script": {
"source": """ctx._source['@timestamp'] = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(ctx._source.process_time.replaceAll(",","."))""",
"lang": "painless"
},
"query": {
"term": {
"@timestamp": "2019-02-01"
}
}
}
但是我得到了以下例外:
"type": "class_cast_exception",
"reason": "Cannot cast java.lang.String to java.util.function.Function"
您可以利用按查詢更新API更新Elasticsearch中所需的文檔。
POST index_name/_update_by_query?conflicts=proceed
{
“script”: {
“source”: “ctx._source[‘@timestamp’] = new SimpleDateFormat(‘yyyy-MM-dd HH:mm:ss,SSS’).parse(ctx._source.process_time)”,
“lang”: “painless”
},
“query”: {
“term”: {
“@timestamp”: “2019-01-01T00:00:00Z”
}
}
}
您正在使用replaceAll方法,但實際上它不是普通的Java String方法。 它是
String replaceAll(Pattern, Function)
無痛API參考更多信息- https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-api-reference.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.