Elasticsearch脚本 - 未定义的变量

时间:2017-03-28 20:20:16

标签: elasticsearch

我正在关注Elasticsearch tutorial,但在尝试在脚本中使用参数时遇到了问题。

第1步:创建新文档 - 确定index = website; type = blog; id = 1

curl -XPUT localhost:9200/website/blog/1?pretty -d '{
  "title":"my first post",
  "tags" : [ "tag1" ]
}'

第2步:使用脚本向tags数组追加额外值 - 错误

curl -XPOST localhost:9200/website/blog/1/_update?pretty -d '{
   "script" : "ctx._source.tags+=new_tag",
   "params" : {
      "new_tag" : "tag2"
   }
}'

错误消息是这样,提到"reason" : "Variable [new_tag] is not defined."但是我已经按照教程页面上的描述定义了变量new_tag。我做错了什么?

  "error" : {
    "root_cause" : [
      {
        "type" : "remote_transport_exception",
        "reason" : "[mrukUvA][127.0.0.1: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 [new_tag] is not defined."
      },
      "script_stack" : [
        "ctx._source.tags+=new_tag",
        "                  ^---- HERE"
      ],
      "script" : "ctx._source.tags+=new_tag",
      "lang" : "painless"
    }
  },
  "status" : 400
}

第2步(重试)new_tag资格params - 错误

curl -XPOST localhost:9200/website/blog/1/_update?pretty -d '{
   "script" : {
       "inline": "ctx._source.tags+=params.new_tag",
       "params" : {
          "new_tag" : "tag2"
       }
   }
}'

给出错误

{
  "error" : {
    "root_cause" : [
      {
        "type" : "remote_transport_exception",
        "reason" : "[mrukUvA][127.0.0.1:9300][indices:data/write/update[s]]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "failed to execute script",
    "caused_by" : {
      "type" : "script_exception",
      "reason" : "runtime error",
      "caused_by" : {
        "type" : "class_cast_exception",
        "reason" : "Cannot cast java.lang.String to java.util.ArrayList"
      },
      "script_stack" : [
        "ctx._source.tags+=params.new_tag",
        "                        ^---- HERE"
      ],
      "script" : "ctx._source.tags+=params.new_tag",
      "lang" : "painless"
    }
  },
  "status" : 400
}

作为完整性检查以确保文档有效

$ curl -XGET localhost:9200/website/blog/1?pretty
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "1",
  "_version" : 27,
  "found" : true,
  "_source" : {
    "title" : "my first post",
    "tags" : [
      "tag1"
    ]
  }
}

因此文档确实具有有效字段tag,这是一个数组。

2 个答案:

答案 0 :(得分:19)

如果您需要$("body").on( "click", "#DOPSelect-DOPBSPCalendar-start-hour1-01:00", function( event ) {}); 脚本所需的参数,那么您的语法略有偏差。试试这个:

inline

答案 1 :(得分:1)

您可以使用“内联”,尽管它已被弃用。现在,您也可以使用“源”替换“内联”,而不会发出警告。例如:

"script" : {
     "source": "ctx._source.tags.add(params.new_tag)",
     "params": {
       "new_tag":"tag1"
     }
   }