我有 6k
的数据要在 ElasticSearch 中更新。而且我必须使用 PHP
。
我在文档中搜索并找到了这个,Bulk Indexing但这并没有保留以前的数据。
我有结构:
[
{
'name': 'Jonatahn',
'age' : 21
}
]
我要更新的代码片段:
$params =[
"index" => "customer",
"type" => "doc",
"body" => [
[
"index" => [
"_index" => "customer",
"_type" => "doc",
"_id" => "09310451939"
]
],
[
"name" => "Jonathan"
]
]
];
$client->bulk($params);
当我发送 ['name' => 'Jonathan']
时,我希望 name
会更新并保持 age
,但是age
被删除。
当然,我仍然可以逐个更新数据,但这需要很长时间,有没有更好的方法来做到这一点?
最佳答案
我的错误是使用"index"
,但正确的方法是"update"
。
最终代码为:
$params =[
"index" => "customer",
"type" => "doc",
"body" => [
[
"update" => [
// ^^^^^^ Here I change from index to update
"_index" => "customer",
"_type" => "doc",
"_id" => "09310451939"
]
],
[
"doc" => [
"name" => "Jonathan"
]
]
]
];
$client->bulk($params);
使用上面的代码,我的数据保留了以前的数据,只是更新了我传入的参数。
响应:
Array
(
[took] => 7
[timed_out] =>
[_shards] => Array
(
[total] => 5
[successful] => 5
[skipped] => 0
[failed] => 0
)
[hits] => Array
(
[total] => 1
[max_score] => 1
[hits] => Array
(
[0] => Array
(
[_index] => customer
[_type] => doc
[_id] => 09310451939
[_score] => 1
[_source] => Array
(
[name] => Jonathan
[age] => 23
)
)
)
)
)
关于php - Elasticsearch 部分批量更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47759682/