【问题标题】:Elasticsearch Update by QueryElasticsearch 查询更新
【发布时间】:2016-10-26 23:04:42
【问题描述】:

我正在尝试根据 ES 版本 2.3.4 的搜索查询更新几个文档。我的用例是搜索两个字段与特定值匹配的文档,然后添加具有特定值的新字段。因此,假设我想搜索名字为“John”和姓氏为“Smith”的所有员工,并在他们的个人资料中添加一个新字段“job”,其中包含值“Engineer”。 所以我的第一个问题是是否可以使用 update_by_query API 的“doc”选项来执行此操作(与更新 API 相同)。

如果不是,并且必须使用脚本(这是我现在这样做的方式),那么也许有人可以帮助我摆脱以下错误:

{"error":{"root_cause":[{"type":"class_cast_exception","reason":"java.lang.String cannot be cast to java.util.Map"}],"type":"class_cast_exception","reason":"java.lang.String cannot be cast to java.util.Map"},"status":500}

我使用的代码如下:

curl -XPOST -s 'http://localhost:9200/test_index/_update_by_query?conflicts=proceed' -d'
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"match": { "first_name" : "John" }
},
{
"match": { "last_name" : "Smith" }
}
]
}
}
}
},
"script" : "ctx._source.job = \"Engineer\""
}'

当使用 count API 发送相同的查询(没有“脚本”字段)时,不会报告错误,并且会重新调整正确数量的文档。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    正确的语法是这样的:

      "script": {
        "inline": "ctx._source.job = \"Engineer\""
      }
    

    【讨论】:

    • 感谢您的回答。但是现在我收到一个错误,即更新类型的 groovy 脚本被禁用。您知道如何启用它们吗?错误的语法如下:{"error":{"root_cause":[{"type":"script_exception","reason":"scripts of type [inline], operation [update] and lang [groovy] are disabled"}],"type":"script_exception","reason":"scripts of type [inline], operation [update] and lang [groovy] are disabled"},"status":500} 另一个问题:是否可以将字段名称作为嵌套字段(例如,用“HR.job”代替“job”?
    • 在所有节点上的elasticsearch.yml 文件中,您需要拥有script.engine.groovy.inline.update: true 并且节点需要重新启动。
    • 关于HR.job 我想是的,不是100%肯定。
    猜你喜欢
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 2016-07-18
    • 2017-01-02
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 2017-09-06
    相关资源
    最近更新 更多