1. 安装:
    composer require elasticsearch/elasticsearch
  2. 使用:
    <?php
    use Elasticsearch\ClientBuilder;
    require './vendor/autoload.php';
    class ES{
     private $esObj;
     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()];
         }
     }
     // 根据 关键词 在指定 key 中 搜索内容
     public function search($index,$keywords,$keys,$page = 1,$size = 10){
         $from = ($page - 1) * $size;
         $params = [
             'index' => $index,
             'body'  => [
                 'query' => [
                     'multi_match' => [
                         'query' => $keywords,
                         'fields' => $keys
                     ]
                 ]
             ],
             'size' => $size,
             'from' => $from,
         ];
         try{
             $response = $this->esObj->search($params);
             return [1,$response['hits']['hits']];
         }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.0.11',
         'port' => '9200',
         'user' => 'elastic',
         'pass' => '123456'
     ],
    ];
    $obj = new ES($hosts);
    for($i = 1;$i <= 100;$i ++){
     $ret1 = $obj->index('mydb',$i,['k1' => '早起的鸟儿有虫吃! - ' . $i,'k2' => '太阳从东方升起! - ' . $i]);
     var_dump($ret1);
    }
    $ret2 = $obj->get('mydb','1');
    var_dump($ret2);
    $ret3 = $obj->search('mydb','鸟',['k1','k2'],5,2);
    var_dump($ret3);
    $ret4 = $obj->deleteId('mydb','1');
    var_dump($ret4);
    $ret5 = $obj->deleteIndex('mydb');
    var_dump($ret5);
  3. 输出:
    array(2) {
     [0]=>
    int(1)
    [1]=>
    string(7) "updated"
    }
    array(2) {
     [0]=>
    int(1)
    [1]=>
    array(2) {
         ["k1"]=>
     string(27) "早起的鸟儿有虫吃!"
         ["k2"]=>
     string(24) "太阳从东方升起!"
    }
    }
    array(2) {
     [0]=>
    int(1)
    [1]=>
    array(1) {
         [0]=>
     array(5) {
             ["_index"]=>
       string(4) "mydb"
             ["_type"]=>
       string(4) "_doc"
             ["_id"]=>
       string(1) "1"
             ["_score"]=>
       float(0.18232156)
       ["_source"]=>
       array(2) {
                 ["k1"]=>
         string(27) "早起的鸟儿有虫吃!"
                 ["k2"]=>
         string(24) "太阳从东方升起!"
       }
     }
    }
    }
    array(2) {
     [0]=>
    int(1)
    [1]=>
    string(7) "deleted"
    }
    array(2) {
     [0]=>
    int(1)
    [1]=>
    array(1) {
         ["acknowledged"]=>
     bool(true)
    }
    }
文档更新时间: 2021-05-31 08:56   作者:lee