Elasticsearch 安装及PHP使用

2021-02-13 04:16

阅读:591

标签:存在   ima   灵活   bre   结果   height   add   desc   mamicode   

ElasticSearch需要基于Java下运行,故首先保证已安装java

此处 mac 使用brew

brew cask install java

安装ElasticSearch

brew 安装 

brew install ElasticSearch

终端输入elasticsearch 运行, 浏览器访问127.0.0.1:9200,出现下图,OK安装完成

技术图片

 

composer 安装引用 elasticsearch

composer.json  require中 加入

"elasticsearch/elasticsearch": "~6.0"

 

PHP 实现elasticsearch基本操作类

php
use Elasticsearch\ClientBuilder;
class ES {
    private $client;
    public function __construct(){
        $ci = get_instance();
        $params = array(
            ‘127.0.0.1:9200‘
        );
        $this->client = ClientBuilder::create()->setHosts($params)->build();
    }

    //创建索引
    function createIndex($index_name = ‘aki‘){
        $params = [
            ‘index‘ => $index_name,
            ‘body‘ => [
                ‘settings‘ => [
                    ‘number_of_shards‘ => 5,
                    ‘number_of_replicas‘ => 0
                ]
            ]
        ];
        return $this->client->indices()->create($params);
    }

    // 删除索引
    public function delIndex($index_name = ‘aki‘) {
        $params = [‘index‘ => $index_name];
        return $this->client->indices()->delete($params);
    }

    // 添加文档
    public function addDoc($id,$doc,$index_name = ‘aki‘,$type_name = ‘form‘) {
        $params = [
            ‘index‘ => $index_name,
            ‘type‘ => $type_name,
            ‘id‘ => $id,
            ‘body‘ => $doc
        ];
        return $this->client->index($params);
    }

    // 判断文档存在
    public function existsDoc($id = 1,$index_name = ‘aki‘,$type_name = ‘form‘) {
        $params = [
            ‘index‘ => $index_name,
            ‘type‘ => $type_name,
            ‘id‘ => $id
        ];

        return $this->client->exists($params);
    }

    // 获取文档
    public function getDoc($id = 1,$index_name = ‘aki‘,$type_name = ‘form‘) {
        $params = [
            ‘index‘ => $index_name,
            ‘type‘ => $type_name,
            ‘id‘ => $id
        ];

        return $this->client->get($params);

    }

    // 更新文档
    public function updateDoc($id,$doc,$index_name = ‘aki‘,$type_name = ‘form‘) {
        // 可以灵活添加新字段,最好不要乱添加
        $params = [
            ‘index‘ => $index_name,
            ‘type‘ => $type_name,
            ‘id‘ => $id,
            ‘body‘ => [
                ‘doc‘ =>$doc
            ]
        ];

        return $this->client->update($params);
    }

    // 删除文档
    public function delDoc($id,$index_name = ‘aki‘,$type_name = ‘form‘) {
        $params = [
            ‘index‘ => $index_name,
            ‘type‘ => $type_name,
            ‘id‘ => $id
        ];

        return $this->client->delete($params);
    }// 查询文档 (分页,排序,权重,过滤)
    public function searchDoc($keywords = "竞赛",$index_name = "aki",$type_name = "form",$sort = [],$from = 0,$size = 10) {
        $params = [
            ‘index‘ => $index_name,
            ‘type‘ => $type_name,
            ‘body‘ => [
                ‘query‘ => [
                    ‘bool‘ => [
                        ‘should‘ => [
                            [ ‘match‘ => [ ‘title‘ => [
                                ‘query‘ => $keywords,
                                ‘boost‘ => 3, // 权重大
                            ]]],
                            [ ‘match‘ => [ ‘content‘ => [
                                ‘query‘ => $keywords,
                                ‘boost‘ => 2,
                            ]]],
                        ],
                    ],
                ],
                ‘sort‘ => [‘price‘=>[‘order‘=>‘desc‘]]
                , ‘from‘ => $from, ‘size‘ => $size
            ]
        ];

        $results = $this->client->search($params);
//        $maxScore  = $results[‘hits‘][‘max_score‘];
//        $score = $results[‘hits‘][‘hits‘][0][‘_score‘];
//        $doc   = $results[‘hits‘][‘hits‘][0][‘_source‘];
        return $results[‘hits‘][‘hits‘];
    }
}

 

测试使用:

     ll(‘es‘);//加载ES类

        $r = $this->es->delIndex();
        $r = $this->es->createIndex();
        print_r($r);

        $docs = [];
        $docs[] = [‘id‘=>1,‘title‘=>‘李白‘,‘content‘=>‘酒仙刺客‘,‘price‘=>100];
        $docs[] = [‘id‘=>2,‘title‘=>‘孙悟空‘,‘content‘=>‘腾云驾雾的辜负了紫霞的刺客。‘,‘price‘=>300];
        $docs[] = [‘id‘=>3,‘title‘=>‘胡歌‘,‘content‘=>‘尽职励志,不错哦。‘,‘price‘=>1000000];
        $docs[] = [‘id‘=>4,‘title‘=>‘王者荣耀‘,‘content‘=>‘游戏就玩王者荣耀。‘,‘price‘=>998];
        $docs[] = [‘id‘=>5,‘title‘=>‘鲁班‘,‘content‘=>‘小短腿,谁都想灭。‘,‘price‘=>98];
        $docs[] = [‘id‘=>6,‘title‘=>‘妲己‘,‘content‘=>‘祸国殃民。‘,‘price‘=>998];
        $docs[] = [‘id‘=>7,‘title‘=>‘吕布‘,‘content‘=>‘方天画戟,后手放大‘,‘price‘=>2998];
        $docs[] = [‘id‘=>8,‘title‘=>‘水晶‘,‘content‘=>‘保护我方水晶,进攻地方水晶。‘,‘price‘=>19999];

        foreach ($docs as $k => $v) {
            $this->es->addDoc($v[‘id‘],$v);
        }
     
     $r = $this->es->searchDoc("李白 歌 水");
    
     echo "
";
     print_r($r);
 

输出结果:

技术图片

 

Elasticsearch 安装及PHP使用

标签:存在   ima   灵活   bre   结果   height   add   desc   mamicode   

原文地址:https://www.cnblogs.com/yimingwang/p/13024436.html


评论


亲,登录后才可以留言!