Elasticsearch 安装及PHP使用

Posted 星辰一幕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch 安装及PHP使用相关的知识,希望对你有一定的参考价值。

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 "<pre>";
     print_r($r);
 

输出结果:

linux 

java jdk: yum install java-1.8.0-openjdk-devel.x86_64

安装 es  https://www.jianshu.com/p/1bf398735dd4  

注意修改  /etc/security/limits.conf,追加以下内容;
* soft nofile 65536
* hard nofile 65536 后退出终端重新登录

守护进程启动  加  -d  

以上是关于Elasticsearch 安装及PHP使用的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch全文检索技术 一篇文章即可从入门到精通(Elasticsearch安装,安装kibana,安装ik分词器,数据的增删改查,全文检索查询,聚合aggregations)(代码片

如何升级到python3版本并且安装pip3及ipython3

linux上安装Elasticsearch

ElasticSearch-7.10版本最新万字长文教程距离搞懂ELK核心你只差这一片文章

使用composer安装Elasticsearch-php教程

使用composer安装Elasticsearch-php教程