我正在关注Elasticsearch 教程,但在尝试在脚本中使用参数时遇到了问题.
第1步:创建一个新文档 - 确定(index = website; type = blog; id = 1)
curl -XPUT localhost:9200/website/blog/1?pretty -d '{
"title":"my first post",
"tags" : [ "tag1" ]
}'
Run Code Online (Sandbox Code Playgroud)
第2步:使用脚本向tags数组附加一个额外的值- 错误
curl -XPOST localhost:9200/website/blog/1/_update?pretty -d '{
"script" : "ctx._source.tags+=new_tag",
"params" : {
"new_tag" : "tag2"
}
}'
Run Code Online (Sandbox Code Playgroud)
错误信息是这样,提到"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
}
Run Code Online (Sandbox Code Playgroud)
步骤2(重试)排位new_tag与params- ERROR
curl -XPOST localhost:9200/website/blog/1/_update?pretty -d '{
"script" : {
"inline": "ctx._source.tags+=params.new_tag",
"params" : {
"new_tag" : "tag2"
}
}
}'
Run Code Online (Sandbox Code Playgroud)
给出错误
{
"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
}
Run Code Online (Sandbox Code Playgroud)
作为完整性检查以确保文档有效
$ 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"
]
}
}
Run Code Online (Sandbox Code Playgroud)
所以文档确实有一个有效的字段tag,它是一个数组.
Val*_*Val 21
如果您需要inline脚本所需的参数,那么您的语法略有偏差.试试这个:
curl -XPOST localhost:9200/website/blog/1/_update?pretty -d '{
"script" : {
"inline": "ctx._source.tags.add(params.new_tag)",
"params" : {
"new_tag" : "tag2"
}
}
}'
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用“内联”,尽管它已被弃用。现在您也可以使用“source”来替换“inline”而没有警告。例如:
"script" : {
"source": "ctx._source.tags.add(params.new_tag)",
"params": {
"new_tag":"tag1"
}
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7153 次 |
| 最近记录: |