我尝试使用更新API。我插入了一个包含列表的文档。
插:
curl -XPOST "http://localhost:9200/t/t/1" -d'
{
"hobbies(first)" : ["a", "b"]
}'
更新查询:
curl -XPOST localhost:9200/t/t/1/_update?pretty -d '{ "script" : {
"inline": "ctx._source.hobbies(first).add(params.new_hobby)",
"params" : {
"new_hobby" : "c"
}
}
}'
错误:
{
"error" : {
"root_cause" : [
{
"type" : "remote_transport_exception",
"reason" : "[aaBiwwv][172.17.0.2:9300][indices:data/write/update[s]]"
}
],
"type" : "illegal_argument_exception",
"reason" : "failed to execute script",
"caused_by" : {
"type" : "script_exception",
"reason" : "compile error",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Variable [first] is not defined."
},
"script_stack" : [
"ctx._source.hobbies(first).add(params.new_hob ...",
" ^---- HERE"
],
"script" : "ctx._source.hobbies(first).add(params.new_hobby)",
"lang" : "painless"
}
},
"status" : 400
}
当我尝试更新列表时,上面出现错误。我已经意识到,当我从字段名称中删除带有方括号('(first)')的零件时,它就起作用了。如何准备带有方括号的字段名称的更新查询?
提前致谢。
最佳答案
这是一个可怕的字段名称约定,只是坚持使用字母数字(请记住,您必须维护一个人,因此在Elasticsearch中使用更干净的数据会更好得多)。您可以尝试ctx.source['hobbies(first)']
关于elasticsearch - Elasticsearch不允许使用括号将字段名称用于脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45899789/