gpt4 book ai didi

elasticsearch - 部分更新Elasticsearch索引时的NPE

转载 作者:行者123 更新时间:2023-12-02 22:57:17 27 4
gpt4 key购买 nike

我遵循here上的示例,使用Elasticsearch中的部分更新来更新一组标签。

以下是我的脚本:

{
"script": {
"lang": "painless",
"inline": "ctx._source.deviceTags.add(params.tags)",
"params": {
"tags": "search"
}
}
}

请求网址为:
https://aws-es-service-url/devices/device/123/_update

但我得到以下回应:
{
"error": {
"root_cause": [
{
"type": "remote_transport_exception",
"reason": "[fBaExM8][x.x.x.x:9300][indices:data/write/update[s]]"
}
],
"type": "illegal_argument_exception",
"reason": "failed to execute script",
"caused_by": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"ctx._source.deviceTags.add(params.tags)",
" ^---- HERE"
],
"script": "ctx._source.deviceTags.add(params.tags)",
"lang": "painless",
"caused_by": {
"type": "null_pointer_exception",
"reason": null
}
}
},
"status": 400
}

我做错了什么吗?

最佳答案

由于您的deviceTags数组最初为null,因此您可以通过两种方式解决此问题

A.使用upsert确保deviceTags最初添加到您的文档中

{
"script": {
"lang": "painless",
"inline": "ctx._source.deviceTags.add(params.tags)",
"params": {
"tags": "search"
}
},
"upsert": {
"deviceTags": ["search"]
}
}

B.保护您的代码不受NPE的侵害
{
"script": {
"lang": "painless",
"inline": "(ctx._source.deviceTags = ctx._source.deviceTags ?: []).add(params.tags)",
"params": {
"tags": "search"
}
}
}

关于elasticsearch - 部分更新Elasticsearch索引时的NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48071931/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com