我将这些数据作为示例
{
"_index": "tags",
"_type": "tag",
"_id": "AVhDUH51AIyDmaqCQz3I",
"_score": 1,
"_source": {
"tagname": "tag1",
"tagnameFr": "tag1fr",
"description": "Some description",
"descriptionFr": "Some description fr",
"contests": [
{
"id": 22
},
{
"id": 33
},
{
"id": 44
}
],
"deal": null,
"category": null
}
}
我这样做是为了更新嵌套文档而且效果很好
// $column = 'contests'; $data = ['contest' => array('id' => 1, 'name' => 'test')];
$this->params['body'] = array(
'script' => array(
"inline" => "ctx._source." . $column . ".add(params." . $column . ")",
"lang" => "painless",
"params" => $data
)
);
但是当我尝试像这样删除时
// $list = 'contests'; and $value = array('id' => 1, 'name' => 'test');
$this->params['body'] = array(
"script" => "ctx._source." . $list . ".remove(params.item)",
"lang" => "painless",
"params" => array(
'item' => $value
)
);
当我执行上面的代码时,我收到此消息/
Connection.php第681行中的BadRequest400Exception: { “错误”:{ “ROOT_CAUSE”:[{ “类型”: “remote_transport_exception”, “理由”: “[56NCJVE] [127.0.0.1:9300] [指数:数据/写/更新[S]]”}] ,“type”:“illegal_argument_exception”,“reason”:“执行脚本失败”,“caused_by”:{“type”:“script_exception”,“reason”:“运行时错误”,“caused_by”:{“type” :“null_pointer_exception”,“reason”:null},“script_stack”:[“ctx._source.contest.remove(params.item)”,“^ ---- HERE”],“script”:“ctx._source .contest.remove(params.item)”, “朗”: “无痛”}}, “状态”:400}
我也尝试在我的代码中执行此脚本,但是我得到了与上面相同的错误。
{
"script": "item_to_remove = nil; foreach (item : ctx._source.list) { if (item['tweet_id'] == tweet_id) { item_to_remove=item; } } if (item_to_remove != nil) ctx._source.list.remove(item_to_remove);",
"params": {"tweet_id": "123"}
}
我用update
$this->client->update($this->params);