Elasticsearch-PHP

文档的增删改查

 


use Elasticsearch\ClientBuilder;
protected $client; protected $index = 'mpids'; protected $type = '_doc'; //实力化es $esparams = array( '192.168.1.177:9200' ); $this->client = ClientBuilder::create()->setHosts($esparams)->build();
添加:
  $params = [
          'index' => 'my_index2',
          'type' => 'my_type',
          'id' => '1', // 如果不提供id,es会自动生成
          'body' => ['first_name' => 'abc']
    ];
    $response =  $this->client->index($params); 
查询:
 $params = [
    'index' => 'my_index2',
    'type' => 'my_type',
    'id' => '1'
];
    $response =  $this->client->get($params);

修改:(根据_id 修改) update
  $esparams1 = [
      'index' => 'mpids',
      'type' => '_doc',
      'id' => '_IJbym4BLw7fgdwICkgL',//修改条件
      'body' => [
        'doc' => [
          'misinformation' => '1', //修改的字段数据
        ]
      ]
  ]
  $response =  $this->client->update($esparams1);
修改:(根据条件批量 修改) updateByQuery
   $where[] = ['match'=>[
           'rules_id' =>[
              'query' =>'2003068',
          ]]];
   $esparams1 = [
            'index' => 'mpids',
            'type' => '_doc', 
            'body' => [
              'query' => [
                'bool' => [ 'must'=>[$where] ]
               ],
              'script' => [
                 "inline"=> "ctx._source.misinformation = params.misinformation",
                 "params"=> ["misinformation"=> 0,],
                 "lang"=>"painless"
               ]
            ],
           ];
  $response =  $this->client->updateByQuery($esparams1);

  

  

posted @ 2019-12-11 12:17  星蛤他叔  阅读(120)  评论(0)    收藏  举报