【问题标题】:cannot resolve symbol[string] when using updateByQuery with ElasticSearch将 updateByQuery 与 ElasticSearch 一起使用时无法解析符号 [字符串]
【发布时间】:2021-08-10 22:57:32
【问题描述】:

我有以下设置:

映射:

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

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

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

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

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

非常感谢您的帮助。

【问题讨论】:

    标签: elasticsearch elastic-stack elk elasticsearch-painless update-by-query


    【解决方案1】:

    您需要用' ' 包围新的lastName 字段值

    添加一个工作示例

    索引数据:

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

    查询:

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

    查询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"
      }
    }
    

    【讨论】:

    • @Lucas Erlacher 请仔细阅读答案,如果这能解决您的问题,请告诉我?
    • 嗨@ESCoder。非常感谢您抽出宝贵时间和回复。我尝试按照您的建议进行操作,但仍然遇到相同的错误。这很奇怪。
    • @LucasErlacher 这很奇怪:\我可以知道您使用的是哪个版本的弹性搜索吗?我在 ES 版本 7.12 中测试了上面的查询,它工作正常
    • @LucasErlacher 您在问题中指出的错误不是您得到的更新错误。正确吗?
    • 嗨@ESCoder。我在 Node 和 ES 的控制台上尝试了你的解决方案,它可以工作。奇怪的是它没有与 Mac 的终端。再次非常感谢。想投票给你的答案,但我还差 15 名声望才能投票。祝您度过愉快的一周。
    猜你喜欢
    • 2020-05-01
    • 1970-01-01
    • 2014-06-20
    • 2018-11-13
    • 2013-12-07
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多