Elasticsearch partial bulk update
My error was to using "index"
, but the correct way to do what I want, was "update"
.
The final code is:
$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);
Using the code above, my data keep previous data and just update the data I passing in params.
Response:
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 ) ) ) ))
As per docs, Bulk API possible actions are index, create, delete and update
. update
expects that the partial doc, upsert and script and its options are specified on the next line.
POST _bulk{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test"} }{ "doc" : {"field2" : "value2"} }
Here is my final code.
<?phprequire_once('../elasticsearch.php');//initialize elasticsearch$params = array();$params['index'] = $elastcsearch_index;$params['type'] = $elastcsearch_type;/////////////////////////////////////////////////////update seeders n leechers in elasticsearch //get updated records$get_updated_records = mysqli_query($conn, "SELECT content_id, seeders, leechers FROM content WHERE is_updated = '1' order by seeders DESC") ;//create blank array$results = array();while($row = mysqli_fetch_assoc($get_updated_records)){ //put all results in array $results[] = $row;} //from https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_indexing_documents.html$params = ['body' => []];for($i = 0; $i < count($results); $i++) { $params["body"][]= [ "update" => [ "_index" => $elastcsearch_index, "_type" => $elastcsearch_type, "_id" => $results[$i]['content_id'] ] ]; $params["body"][]= [ "doc" => [ "seeders" => intval($results[$i]['seeders']) , "leechers" => intval($results[$i]['leechers']) , ] ]; // Every 1000 documents stop and send the bulk request if ($i % 1000 == 0) { $responses = $elasticsearch->bulk($params); // erase the old bulk request $params = ['body' => []]; // unset the bulk response when you are done to save memory unset($responses); } }// Send the last batch if it existsif (!empty($params['body'])) { $responses = $elasticsearch->bulk($params);}