Elasticsearch,为什么添加的Element总是null
Elasticsearch, why is added Element always null
我有一个 Elasticsearch-DB,想向数组中添加元素(字符串)。根据这里的讨论 https://discuss.elastic.co/t/append-to-existing-field/16423/2 我建立了一个玩具模型。
我初始化:
curl -XPOST "http://localhost:9200/t/t/1/" -d'
{
"hobbies" : ["a", "b"]
}'
并使用
更新数组
curl -XPOST "http://localhost:9200/t/t/1/_update" -d'
{
"script" : "ctx._source.hobbies.add(params.hobby)",
"params" : {
"hobby" : "c"
}
}'
不幸的是,更新后的结果总是 "null" 而不是 "c":
curl -XGET 'http://localhost:9200/_search?q=_id:"1"&pretty'
{
"took" : 29,
"timed_out" : false,
"_shards" : {
"total" : 20,
"successful" : 20,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "t",
"_type" : "t",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"hobbies" : [
"a",
"b",
null,
null,
null
]
}
}
]
}
}
我做错了什么?
你的script
结构不正确,应该是这样的:
curl -XPOST "http://localhost:9200/t/t/1/_update" -d'
{
"script" : {
"inline": "ctx._source.hobbies.add(params.hobby)",
"params" : {
"hobby" : "c"
}
}
}'
我有一个 Elasticsearch-DB,想向数组中添加元素(字符串)。根据这里的讨论 https://discuss.elastic.co/t/append-to-existing-field/16423/2 我建立了一个玩具模型。 我初始化:
curl -XPOST "http://localhost:9200/t/t/1/" -d'
{
"hobbies" : ["a", "b"]
}'
并使用
更新数组curl -XPOST "http://localhost:9200/t/t/1/_update" -d'
{
"script" : "ctx._source.hobbies.add(params.hobby)",
"params" : {
"hobby" : "c"
}
}'
不幸的是,更新后的结果总是 "null" 而不是 "c":
curl -XGET 'http://localhost:9200/_search?q=_id:"1"&pretty'
{
"took" : 29,
"timed_out" : false,
"_shards" : {
"total" : 20,
"successful" : 20,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "t",
"_type" : "t",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"hobbies" : [
"a",
"b",
null,
null,
null
]
}
}
]
}
}
我做错了什么?
你的script
结构不正确,应该是这样的:
curl -XPOST "http://localhost:9200/t/t/1/_update" -d'
{
"script" : {
"inline": "ctx._source.hobbies.add(params.hobby)",
"params" : {
"hobby" : "c"
}
}
}'