【问题标题】:How to update a string field with elasticsearch _update_by_query?如何使用 elasticsearch _update_by_query 更新字符串字段?
【发布时间】:2017-06-23 16:12:15
【问题描述】:

我的索引已经填满了许多文档。
索引中的所有文档都有一个name 字符串字段。
我想查询并更新所有具有name = A 并将其设置为name = B

为了清楚起见,在 SQL 中它会类似于:
UPDATE FROM table SET name = 'B' WHERE name = 'A';

使用 elasticsearch API,根据文档: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html

我试过这个查询:

POST my_index/_update_by_query
{
  "script": {
    "inline": "ctx._source.name = 'B'",
    "lang": "painless"
  },
  "query": {
    "term": {
      "name" : "A"
    }
  }
}

但是它只是返回没有修改任何内容。我仍然可以找到 name=A 的文档,因此它们没有被编辑。

{
  "took": 1,
  "timed_out": false,
  "total": 0,
  "updated": 0,
  "deleted": 0,
  "batches": 0,
  "version_conflicts": 0,
  "noops": 0,
  "retries": {
    "bulk": 0,
    "search": 0
  },
  "throttled_millis": 0,
  "requests_per_second": -1,
  "throttled_until_millis": 0,
  "failures": []
}

知道为什么我的 _update_by_query 没有做任何事情吗?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    找到了,我的字符串字段是分析字段,所以我必须使用这样的匹配查询:

    POST my_index/_update_by_query
    {
      "script": {
        "inline": "ctx._source.name = 'B'",
        "lang": "painless"
      },
      "query": {
        "match": {
          "name" : "A"
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多