zoukankan      html  css  js  c++  java
  • 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 ElasticsearchClientBuilder;
    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  

  • 相关阅读:
    HttpClientUtil的工具类请求三方API
    Linux下使用docker搭建ftp服务器
    Springboot2.0.4整合Mybatisplus
    Springboot前后端分离项目,配置跨域
    Nokia S40 系统配置
    "Your profile could not be opened correctly" Google Chromium Browser 错误纠正
    CLR AppDomain
    iTunes下载提速
    Which Programming Language?
    linux下制作软盘镜像文件
  • 原文地址:https://www.cnblogs.com/yimingwang/p/13024436.html
Copyright © 2011-2022 走看看