laravel elasticsearch 批量导入与批量删除

在laravel 中使用了 elasticsearch/search 。

批量增加:

 
$req=['body'=>[]]; foreach ($videos as $video) { $this->info('正在同步:'.$video->video_id.' 用户标识: '.$video->user_id); $data=$video->toESArray(); /*返回null时,是因为用户删除了,关联删除没有删除的问题,及时删除视频*/ if($data==null) { continue; } $req['body'][]=['index'=>['_index'=>'video_list','_id'=>$data['video_id']]]; $req['body'][]=$data; } try { $es->bulk($req); } catch (Exception $e) { $this->info($e->getMessage()); }

批量删除:

  /*$ids为id array,如  [2,3]*/
$req=['body'=>[]];
               foreach ($ids as $id)
               {
                   $req['body'][]=['delete'=>['_index'=>'video_list','_id'=>$id]];
               }
               try
               {
                   app('es')->bulk($req);
               }
               catch (Exception $e)
               {
                  echo  $e->getMessage();
               }
原文地址:https://www.cnblogs.com/fogwang/p/12739211.html