1. 安装:
    composer require elasticsearch/elasticsearch
  2. 使用:
    <?php
    use Elasticsearch\ClientBuilder;
    require './vendor/autoload.php';
    class ES{
     private $esObj;
     public $group_by_field = 'group_by_field';
     public $aggs_field = 'aggs_field';
     private $sql = [];
     public function __construct($hosts){
         $obj = new ClientBuilder();
         $obj->setHosts($hosts);
         $this->esObj = $obj->build();
     }
     // 添加/更新 index
     public function index($index,$id,$body){
         $params = [
             'index' => $index,
             'id'    => $id,
             'body'  => $body
         ];
         try{
             $response = $this->esObj->index($params);
             return [1,$response['result']];
         }catch (Exception $e){
             return [0,$e->getMessage()];
         }
     }
     // 根据 id 获取内容
     public function get($index,$id){
         $params = [
             'index' => $index,
             'id'    => $id
         ];
         try{
             $response = $this->esObj->getSource($params);
             return [1,$response];
         }catch (Exception $e){
             return [0,$e->getMessage()];
         }
     }
     public function order($data){
         $this->sql['sort'] = $data;
         return $this;
     }
     public function matchAll(){
         $this->sql['query']['match_all'] = new stdClass();
         return $this;
     }
     public function page($page,$size){
         $from = ($page - 1) * $size;
         $this->sql['from'] = $from;
         $this->sql['size'] = $size;
         return $this;
     }
     public function match($data){
         $this->sql['query']['match'] = $data;
         return $this;
     }
     public function matchPhrase($data){
         $this->sql['query']['match_phrase'] = $data;
         return $this;
     }
     public function mustMatch($data){
         $this->sql['query']['bool']['must'] = [
             [
                 'match' => $data,
             ]
         ];
         return $this;
     }
     public function mustNotMatch($data){
         $this->sql['query']['bool']['must_not'] = [
             [
                 'match' => $data,
             ]
         ];
         return $this;
     }
     public function range($data){
         $this->sql['query']['bool']['must']['match_all'] = new stdClass();
         $this->sql['query']['bool']['filter']['range'] = $data;
         return $this;
     }
     // ***************** 聚合搜索: BEGIN ***************** //
     public function groupBy($field){
         $this->sql['aggs'][$this->group_by_field]['terms']['field'] = $field . '.keyword';
         $this->sql['size'] = 0;
         return $this;
     }
     public function aggs($operator,$field){
         $this->sql['aggs'][$this->group_by_field]['aggs'][$this->aggs_field][$operator]['field'] = $field;
         return $this;
     }
     public function groupOrder($sort){
         $this->sql['aggs'][$this->group_by_field]['terms']['order'][$this->aggs_field] = $sort;
         return $this;
     }
     // ****************** 聚合搜索: END ****************** //
     // 根据 关键词 在指定 key 中 搜索内容
     public function search($index){
         $body = json_encode($this->sql);
         $params = [
             'index' => $index,
             'body'  => $body,
         ];
         try{
             $response = $this->esObj->search($params);
             return [1,$response];
         }catch (Exception $e){
             return [0,$e->getMessage()];
         }
     }
     // 删除指定 ID
     public function deleteId($index,$id){
         $params = [
             'index' => $index,
             'id'    => $id
         ];
         try{
             $response = $this->esObj->delete($params);
             return [1,$response['result']];
         }catch (Exception $e){
             return [0,$e->getMessage()];
         }
     }
     // 删除指定索引
     public function deleteIndex($index){
         $deleteParams = [
             'index' => $index
         ];
         try{
             $response = $this->esObj->indices()->delete($deleteParams);
             return [1,$response];
         }catch (Exception $e){
             return [0,$e->getMessage()];
         }
     }
    }
    $hosts = [
     [
         'scheme' => 'http',
         'host' => '192.168.1.13',
         'port' => '9200',
         'user' => 'elastic',
         'pass' => 'admin123'
     ],
    ];
    $obj = new ES($hosts);
    for($i = 1;$i <= 100;$i ++){
     $ret1 = $obj->index(
         'mydb',
         $i,
         [
             'k1' => '早起的鸟儿有虫吃! - ' . $i,
             'k2' => '太阳从东方升起! - ' . $i,
             'k3' => $i * 10,
         ]
     );
     var_dump($ret1);
    }
    $ret2 = $obj->index(
     'mydb',
     101,
     [
         'k1' => '张三',
         'k2' => '武汉',
         'k3' => 1010,
     ]
    );
    var_dump($ret2);
    $ret3 = $obj->get('mydb','1');
    var_dump($ret3);
    // 1. match_all / order
    $ret4 = $obj->matchAll()->order(['k3' => 'desc'])->search('mydb');
    var_dump($ret4);
    // 2. match_all / order / page
    $ret5 = $obj->matchAll()->order(['k3' => 'desc'])->page(1,3)->search('mydb');
    var_dump($ret5);
    // 3. match
    $ret6 = $obj->match(['k1' => '张'])->search('mydb');
    var_dump($ret6);
    // 4. match_phrase
    $ret7 = $obj->matchPhrase(['k1' => '张'])->search('mydb');
    var_dump($ret7);
    // 5. must / must_not
    $ret8 = $obj->mustMatch(['k1' => '张'])->mustNotMatch(['k2' => '太'])->search('mydb');
    var_dump($ret8);
    // 6. range
    $ret9 = $obj->range(['k3' => ['gte' => 200,'lte' => 300]])->search('mydb');
    var_dump($ret9);
    // 7. group_by
    $ret10 = $obj->groupBy('k1')->search('mydb');
    var_dump($ret10);
    // 8. group_by / aggs
    $ret11 = $obj->groupBy('k1')->aggs('avg','k3')->search('mydb');
    var_dump($ret11);
    // 9. group_by / aggs / group_by_order
    $ret12 = $obj->groupBy('k1')->aggs('avg','k3')->groupOrder('desc')->search('mydb');
    var_dump($ret12);
    $ret13 = $obj->deleteId('mydb','1');
    var_dump($ret13);
    $ret14 = $obj->deleteIndex('mydb');
    var_dump($ret14);
文档更新时间: 2021-11-26 10:59   作者:lee