簡體   English   中英

通過查詢使用ElasticSearch中的子句更新

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM