update and updateByQuery methods for elasticsearch in PHP

When using ES, we often encounter the situation of updating data, Here is a brief introduction: First, the most common and efficient method is to upd...

When using ES, we often encounter the situation of updating data,
Here is a brief introduction:
First, the most common and efficient method is to update according to id. at this time, the update method is called:

public static function updateEsById($id,$data = []) { if (empty($id) || empty($data)){ return false; } $es = new Elastic(); $data=[ 'index' => self::$es_index, 'type' => self::$es_type, 'id' => $id, 'body' => [ 'doc'=> $data //The data here is a one-dimensional associative array, which is consistent with the common ORM update method parameters. ] ]; $res = $es->update($data); return $res['result']; }

Isn't it simple?
Another situation is that we don't know the id of the data to be updated, or we need to batch update according to the conditions. In this case, we can use the updateByQuery method:

$params = [ 'index' => StatLog::$es_index, 'type' => StatLog::$es_type, 'body' => [ 'query' => [ 'bool' => [ 'must' => [ 0=>['term' => [ 'uuid' =>'147ee4a81cb94471014ecb21ae74a88f' ]], 1=>['term' => [ 'args' =>'http%3A%2F%2Foverhit.wiki.dev.yingxiong.com%2Fcontentmoba%2F2452.html' ]] , 2=>['term' => [ 'add_time' =>1542341128 ]] ] ], ], 'script'=>[ //Update the online time field to the value passed in params, here is 11111 "inline"=> "ctx._source.online_time=params.online_time", //Here, you can realize the requirements such as auto increment of a certain field, for example: // "inline"=> "ctx._source.online_time= ctx._source.online_time params.online_time", 'params'=>['online_time'=>11111], 'lang'=>'painless' ] ] ]; $client = new Elastic(); $res = $client->updateByQuery($params); var_dump($res);

I have multiple conditions here, and all of them are =. Of course, I can also filter them according to the range and other logical operations, such as:

$params = [ 'index' => 'my_index', 'body' => [ 'query' => [ 'bool' => [ 'must' => [ 'range' => [ 'age' => [ 'gt' => '20', 'lt' => '40' ] ] ] ] ], 'script' => [ 'inline' => "ctx._source.name=\"Puber\"; ctx._source.age=30" ] ] ]; $client = Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build(); $res = $client->updateByQuery($params);


Simply encapsulate a PHP method:

/** * Notes : * @param array $where * @param string $field * @param string $value * @return bool * author: leojen * @date: 18-11-21 6:06 p.m. */ public static function upByWhere($where = [],$data = []) { if (empty($where) || empty($data)){ return false; } $fields = ''; foreach ($data as $k=>$v){ $fields .="ctx._source.{$k} = $v;"; } $fields = trim($fields,';'); $params = [ 'index' => self::$es_index, 'type' => self::$es_type, 'body' => [ 'query' => [ 'bool'=>['must'=>[]] ], 'script'=>[ "inline"=> $fields, 'lang'=>'painless' ] ] ]; foreach ($where as $key=>$val){ $params['body']['query']['bool']['must'][] = [ 'term' => [ $key =>$val ] ]; } $client = new Elastic(); return $client->updateByQuery($params); }

http://leojen.cn/

2 December 2019, 05:19 | Views: 1258

Add new comment

For adding a comment, please log in
or create account

0 comments