前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP实现elasticsearch/elasticsearchy实例

PHP实现elasticsearch/elasticsearchy实例

作者头像
OwenZhang
发布2021-12-08 14:48:04
5130
发布2021-12-08 14:48:04
举报
文章被收录于专栏:Owen's World

es中文文档

learnku.com/docs/elasti…

极客时间-Elasticsearch核心技术与实践-课件及 Demo 下载地址

gitee.com/geektime-ge…

第一:安装elasticsearch环境
代码语言:javascript
复制
## 相关阅读
- 安装指南 https://www.elastic.co/guide/en/elasticsearch/reference/7.1/install-elasticsearch.html
- Elastic Support Matrix(OS / JDK ) https://www.elastic.co/cn/support/matrix
- Elasticsearch 的一些重要配置 https://www.elastic.co/guide/en/elasticsearch/reference/current/important-settings.html
- https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html
- https://www.elastic.co/guide/en/elasticsearch/reference/current/important-settings.html
- Elasticsearch on Kuvernetes https://www.elastic.co/cn/blog/introducing-elastic-cloud-on-kubernetes-the-elasticsearch-operator-and-beyond
- CAT Plugins API https://www.elastic.co/guide/en/elasticsearch/reference/7.1/cat-plugins.html

window 的使用,首先下载安装包,百度网盘下载(https://pan.baidu.com/s/1CRT3W4wEESglCBDnslk2AA)
解压到本地,我是D:\elasticsearch-7.1.0

#启动单节点 
bin/elasticsearch -E node.name=node0 -E cluster.name=geektime -E path.data=node0_data
window .\bin\elasticsearch

#安装插件
bin/elasticsearch-plugin install analysis-icu
window .\bin\elasticsearch-plugin install analysis-icu
复制代码
第二:composer require elasticsearch/elasticsearch
第三

单例,InstanceTrait.php

代码语言:javascript
复制
<?php
/**
 * Created by PhpStorm.
 * User: owenzhang
 * Date: 2019/3/4
 * Time: 下午4:30
 */

namespace app\common;

trait InstanceTrait
{
    private static $instances;

    protected function __construct()
    {
    }

    private function __clone()
    {
    }

    /**
     * @return static
     */
    public static function getInstance()
    {
        $className = get_called_class();
        $args = func_get_args();
        //若$args中有resource类型的参数,则无法区分同一个类的不同实例
        $key = md5($className . ':' . serialize($args));
        if (!isset(self::$instances[$key])) {
            //PHP_VERSION >= 5.6.0
            self::$instances[$key] = new $className(...$args);
        }
        return self::$instances[$key];
    }
}
复制代码

服务,ElasticSearchModel.php

代码语言:javascript
复制
<?php
/**
 * ElasticSearch
 */
namespace app\common\model;

use app\common\InstanceTrait;
use Elasticsearch\ClientBuilder;
use Elasticsearch\Common\Exceptions\Missing404Exception;

class ElasticSearchModel
{
    use InstanceTrait;

    private $_devConfig = [
        'sports_search_expert' => [
            'host' => '127.0.0.1',
            'port' => 9200,
            'index' => 'sports_search_expert'
        ],
        'sports_search_blog' => [
            'host' => '127.0.0.1',
            'port' => 9200,
            'index' => 'sports_search_blog'
        ]
    ];

    private $_prodConfig = [
        'sports_search_expert' => [
            'host' => '127.0.0.1',
            'port' => 9200,
            'index' => 'sports_search_expert'
        ],
        'sports_search_blog' => [
            'host' => '127.0.0.1',
            'port' => 9200,
            'index' => 'sports_search_blog'
        ],
    ];

    private $_config;
    private $_client;
    private $_error = false;

    public function __construct($key)
    {
        $this->_config = $this->_devConfig;
        if (env('APP_ENV') == 'prod') {
            $this->_config = $this->_prodConfig;
        }

        if (!array_key_exists($key, $this->_config)) {
            $this->_error = true;
            return;
        }

        $this->_config = $this->_config[$key];
        $hosts = [$this->_config['host'] . ':' . $this->_config['port']];
        $this->_client = ClientBuilder::create()->setHosts($hosts)->build();
    }

    /**
     * 插入数据
     *
     * data['type']
     * data['data']
     */
    public function addDoc($type, $data)
    {
        try {
            if ($this->_error) {
                throw new \Exception('配置错误');
            }
            if (!is_array($data)) {
                throw new \Exception('参数缺失');
            }

            $params = [
                'index' => $this->_config['index'],
                'type' => $type,
                'body' => $data,
            ];

            if (isset($data['id'])) {
                $params['id'] = $data['id'];
            }

            $this->_client->index($params);
            return ['code' => 0, 'msg' => '成功'];
        } catch (\Exception $exception) {
            \Log::error($exception);
            return ['code' => -1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     * 查询数据
     */
    public function getDocById($type, $id)
    {
        try {
            if ($this->_error) {
                throw new \Exception('配置错误');
            }

            $params = [
                'index' => $this->_config['index'],
                'type' => $type,
                'id' => $id
            ];
            return ['code' => 0, 'msg' => '成功', 'data' => $this->_client->get($params)];
        } catch (Missing404Exception $exception) {
            return ['code' => 0, 'data' => []];
        } catch (\Exception $exception) {
            \Log::error($exception);
            return ['code' => -1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     * 根据ID更新数据
     */
    public function updateDoc($type, $id, $data)
    {
        try {
            if ($this->_error) {
                throw new \Exception('配置错误');
            }

            $params = [
                'index' => $this->_config['index'],
                'type' => $type,
                'id' => $id,
                'body' => ['doc' => $data]
            ];

            return ['code' => 0, 'msg' => '成功', 'data' => $this->_client->update($params)];
        } catch (\Exception $exception) {
            \Log::error($exception);
            return ['code' => -1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     * 搜索
     */
    public function searchDoc($type, $body, $page = 1, $size = 10)
    {
        try {
            if ($this->_error) {
                throw new \Exception('配置错误');
            }

            $params = [
                'index' => $this->_config['index'],
                'type' => $type,
                'body' => $body,
                'from' => ($page - 1) * $size,
                'size' => $size
            ];

            $data = $this->_client->search($params);
            $hits = isset($data['hits']) ? $data['hits'] : [];
            $total = isset($hits['total']) ? $hits['total'] : 0;
            $hitsArr = isset($hits['hits']) ? $hits['hits'] : [];
            $list = [];
            foreach ($hitsArr as $value) {
                $list[] = $value['_source'];
            }

            $return = [
                'total' => $total,
                'list' => $list
            ];
            return ['code' => 0, 'msg' => '成功', 'data' => $return];
        } catch (\Exception $exception) {
            \Log::error($exception);
            return ['code' => -1, 'msg' => '暂无数据', 'data' => []];
        }
    }
}
复制代码

实例:SyncBlog.php

添加,更新,获取一个

ElasticSearchModel::getInstance('sports_search_blog')->addDoc('blog', $data);

ElasticSearchModel::getInstance('sports_search_blog')->updateDoc('blog', id,id, id,data);

ElasticSearchModel::getInstance('sports_search_blog')->getDocById('blog', $id);

代码语言:javascript
复制
<?php
/**
 * 同步资讯
 */

namespace app\polymerize\tool\module\es;

use app\common\model\BlogModel;
use app\common\model\ElasticSearchModel;

class SyncBlog
{
    use \app\common\InstanceTrait;

    public function syncBlog($id)
    {
        //获取用户数据
        $blogInfo = BlogModel::getInstance()->getOneById($id, 4, 'sports');
        if (empty($blogInfo)) {
            return ['code' => -1, 'msg' => '不存在该资讯'];
        }

        //判断是否存在
        $result = ElasticSearchModel::getInstance('sports_search_blog')->getDocById('blog', $id);
        if (!empty($result['code'])) {
            return $result;
        }

        //不存在写入
        if (empty($result['data'])) {
            $data = [
                'id' => $id,
                'search' => $blogInfo['title'],
                'status' => $blogInfo['status']
            ];
            ElasticSearchModel::getInstance('sports_search_blog')->addDoc('blog', $data);
            return $result;
        }

        //存在更新
        $data = [
            'search' => $blogInfo['title'],
            'status' => $blogInfo['status']
        ];
        $result = ElasticSearchModel::getInstance('sports_search_blog')->updateDoc('blog', $id, $data);
        return $result;
    }
}
复制代码

分页查找

ElasticSearchModel::getInstance('sports_search_blog')->searchDoc('blog', data,data, data,pageNo, $pageCount);

SearchBlog.php

代码语言:javascript
复制
<?php
/**
 * 搜索资讯
 */
namespace app\polymerize\tool\module\es;

use app\common\InstanceTrait;
use app\common\model\BlogModel;
use app\common\model\ElasticSearchModel;

class SearchBlog
{
    use InstanceTrait;

    public function searchBlog($name, $pageNo, $pageCount)
    {
        $data = [
            'query' => [
                'bool' => [
                    'must' => [
                        'match' => [
                            'search' => $name
                        ]
                    ],
                    'filter' => [
                        'term' => [
                            'status' => 1 //TODO 改成常量
                        ]
                    ],
                ],
            ]
        ];
        $result = ElasticSearchModel::getInstance('sports_search_blog')->searchDoc('blog', $data, $pageNo, $pageCount);

        if ($result['code'] != 0 || empty($result['data'])) {
            return $result;
        }

        $data = $result['data'];
        $list = $data['list'];

        $blogIdArr = array_column($list, 'id');
        $blogInfo = BlogModel::getInstance()->getBlogListByIdArrAndTypeIdArr($blogIdArr);

        $return = [];
        foreach ($list as $value) {
            if (!isset($blogInfo[$value['id']])) {
                continue;
            }

            $return[] = [
                'blog_id' => $value['id'],
                'blog_title' => $blogInfo[$value['id']]['title'],
                'blog_img' => $blogInfo[$value['id']]['thumbnail_url'],
            ];
        }

        $return = [
            'list' => $return,
        ];

        return ['code' => 0, 'data' => $return];
    }
}
复制代码
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年10月25日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 添加,更新,获取一个
  • 分页查找
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档