将 updateByQuery 与 ElasticSearch 一起使用时无法解析符号 [string]

问题描述

我有以下设置:

映射:

esClient.indices.putMapping({
  index: 'tests',body: {
    properties: {
      name: {
        type: 'text',},lastName: {
        type: 'text',});

这是我发布条目时的结果:

enter image description here

这是我查询条目时的结果:

curl -X GET "localhost:9200/tests/_search?pretty" -H 'Content-Type: application/json' -d'       
{
"size": 1000,"query" : {
    "match_all" : {}
  }
}
'
{
  "took" : 18,"timed_out" : false,"_shards" : {
    "total" : 1,"successful" : 1,"skipped" : 0,"Failed" : 0
  },"hits" : {
    "total" : {
      "value" : 1,"relation" : "eq"
    },"max_score" : 1.0,"hits" : [
      {
        "_index" : "tests","_type" : "_doc","_id" : "KJbtj3kBRlqnip7VJLLI","_score" : 1.0,"_source" : {
          "lastName" : 1,"name" : "lucas"
        }
      }
    ]
  }
}

我尝试使用以下 curl 更新条目的姓氏:

curl -X POST "localhost:9200/tests/_update_by_query?pretty" -H 'Content-Type: application/json' -d'
{
  "script": {
    "source": "ctx._source.lastName='johnson'","lang": "painless"
  },"query": {
    "term": {
      "name": "lucas"
    }
  }
}
'

这就是我犯的错误

    {
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception","reason" : "compile error","script_stack" : [
          "ctx._source.lastName=johnson","                     ^---- HERE"
        ],"script" : "ctx._source.lastName=johnson","lang" : "painless","position" : {
          "offset" : 21,"start" : 0,"end" : 28
        }
      }
    ],"type" : "script_exception","script_stack" : [
      "ctx._source.lastName=johnson","                     ^---- HERE"
    ],"position" : {
      "offset" : 21,"end" : 28
    },"caused_by" : {
      "type" : "illegal_argument_exception","reason" : "cannot resolve symbol [johnson]"
    }
  },"status" : 400
}

如果我输入一个整数而不是一个字符串,它会更新它,否则我会一直收到那个错误

非常感谢您的帮助。

解决方法

您需要用 lastName 将新的 ' ' 字段值括起来。

添加一个工作示例

索引数据:

{
    "name":"lucas","lastName":"erla"
}

查询:

POST _update_by_query
{
  "script": {
    "source": "ctx._source.lastName='johnson'","lang": "painless"
  },"query": {
    "term": {
      "name": "lucas"
    }
  }
}

点击update by query API后,文档会更新为

GET /_doc/1

{
  "_index": "67641538","_type": "_doc","_id": "1","_version": 3,"_seq_no": 2,"_primary_term": 1,"found": true,"_source": {
    "lastName": "johnson","name": "lucas"
  }
}