elasticsearch - Elasticsearch不允许使用括号将字段名称用于脚本

标签 elasticsearch special-characters elasticsearch-5

我尝试使用更新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/

相关文章:

elasticsearch - 安装和配置Shield for Elasticsearch

search - 同义词,将权重存储在文档中以在Elastic Search中进行相关性评分

javascript - 读取 jQuery 中的特殊字符

elasticsearch - 简单查询与一个特定字段不匹配

mysql - ElasticSearch如何与Mysql集成

elasticsearch - 将多类型索引迁移到单类型索引

elasticsearch - Elasticsearch每日滚动索引包含重复的_id

windows - Windows 资源管理器使用的排序顺序中的第一个字符是什么?

php - UTF8数据库查询及特殊字符

elasticsearch - 由于以下原因,无法检索群集状态:没有配置的节点可用