我想通过弹性搜索来部分更新现有文档,因此我编写了更新查询,该查询部分更新了文档而不是整个文档 需要更新的示例数据
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 3.0780919,
"hits": [
{
"_index": "trending",
"_type": "doc",
"_id": "bx-1605773",
"_score": 3.0780919,
"_routing": "1",
"_source": {
"id": "bx-1605773",
"name": "new",
"db_id": 1605773,
"user_id": "u-2",
"box_user": {
"id": 2,
"box_id": 1605773,
"username": "yahoo",
"full_name": "Yahoo1",
"is_private": true
},
"status": "M",
"created_at": "2018-08-30T11:58:10Z",
"type": {
"name": "box",
"parent": "u-2"
},
"box_posts": []
}
}
]
}
}
在此文档中,我仅为此目的更新盒子名称和盒子状态,我在ES中编写以下查询
$params = [
'index' => 'trending',
'type' => 'doc',
'id' => $this->prepareId($box->id, 'bx'),
'body' => [
'doc' => [
'name' => $box->name,
]
]
];
try {
$response = $this->client->update($params);
} catch (\Exception $ex) {
return false;
}
但是当我运行此查询时,我收到以下异常
{"error":{"root_cause":[{"type":"document_missing_exception","reason":"[doc][bx-1605773]: document missing","index_uuid":"h8kvjFk7S0usH3YBO-697A","shard":"0","index":"trending"}],"type":"document_missing_exception","reason":"[doc][bx-1605773]: document missing","index_uuid":"h8kvjFk7S0usH3YBO-697A","shard":"0","index":"trending"},"status":404}
即使我在弹性搜索主网站上找到此查询
我不知道我在哪里做错
答案 0 :(得分:1)
您丢失了routing
参数,因为您使用"routing": "1"
为文档建立了索引,因此在更新文档时还需要指定此信息,否则将找不到该文档:
$params = [
'index' => 'trending',
'type' => 'doc',
'id' => $this->prepareId($box->id, 'bx'),
>>> 'routing' => 1,
'body' => [
'doc' => [
'name' => $box->name,
]
]
];
try {
$response = $this->client->update($params);
} catch (\Exception $ex) {
return false;
}