繁体   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