IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> es 基本操作 及 高亮显示 封装接口 -> 正文阅读

[PHP知识库]es 基本操作 及 高亮显示 封装接口

安装Elasticsearch-php: composer require elasticsearch / elasticsearch

?高亮显示插件:?composer require nunomaduro/collision

 //ES客户端链接
    private $client;

    /**
     * 构造函数
     * * MyElasticsearch constructor.
     */
    public function __construct()
    {
        $params = array(
            '127.0.0.1:9200'
        );
        $this->client = ClientBuilder::create()->setHosts($params)->build();
    }


    /**
     * 判断索引是否存在
     * @param string $index_name
     * @return bool|mixed|string
     */
    public function exists_index($index_name = null)
    {
        $params = [
            'index' => $index_name
        ];

        try {
            return $this->client->indices()->exists($params);
        } catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
            $msg = $e->getMessage();
            $msg = json_decode($msg, true);
            return $msg;
        }
    }


    /**
     * 创建索引
     * * @param string $index_name
     * @return array|mixed|string
     */
    public function create_index($index_name = null)
    {// 只能创建一次
        $params = [
            'index' => $index_name,
            'body' => [
                'settings' => [
                    'number_of_shards' => 5,
                    'number_of_replicas' => 0
                ]
            ]
        ];

        try {
            return $this->client->indices()->create($params);
        } catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
            $msg = $e->getMessage();
            $msg = json_decode($msg, true);
            return $msg;
        }

    }


    /**
     * 删除索引
     * @param string $index_name
     * @return array
     */
    public function delete_index($index_name = null)
    {
        $params = ['index' => $index_name];
        $response = $this->client->indices()->delete($params);
        return $response;
    }


    /**
     * 添加文档
     * @param $id
     * @param $doc ['id'=>100, 'title'=>'phone']
     * @param string $index_name
     * @param string $type_name
     * @return array
     */
    public function add_doc($id, $doc, $index_name = 'title_index', $type_name='title' )
    {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id,
            'body' => [
                'title'=>$doc['title'],
                'text'=>$doc['text'],
                'id'=>$doc['id']
            ]

        ];
        $response = $this->client->index($params);
        return $response;
    }


    /**
     * 判断文档存在
     * * @param int $id
     * @param string $index_name
     * @param string $type_name
     * * @return array|bool
     */
    public function exists_doc($id , $index_name = 'title_index', $type_name = 'title')
    {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];
        $response = $this->client->exists($params);
        return $response;
    }


    /**
     * 获取文档
     * * @param int $id
     * @param string $index_name
     * @param string $type_name
     * @return array
     */
    public function get_doc($id, $index_name = 'title_index', $type_name = 'title')
    {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];
        $response = $this->client->get($params);
        return $response;
    }



    /**
     * 删除文档
     * @param int $id
     * @param string $index_name
     * @param string $type_name
     * @return array
     */
    public function delete_doc($id = 1, $index_name = 'title_index', $type_name = 'title')
    {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id,
        ];
        $response = $this->client->delete($params);
        return $response;
    }


    /*** 搜索文档 (分页,排序,权重,过滤)
     * @param string $index_name
     * @param string $type_name
     * @param array $body
     *  $body = [
     * 'query' => [
     * 'bool' => [
     * 'should' => [
     * [
     * 'match' => [
     * 'cate_name' => [
     * 'query' => $keywords,
     * 'boost' => 4, // 权重大
     * ]
     * ]
     * ],
     * [
     * 'match' => [
     * 'goods_name' => [
     * 'query' => $keywords,
     * 'boost' => 3, ]
     * ]
     * ],
     * [
     * 'match' => [
     * 'goods_introduce' => [
     * 'query' => $keywords,
     * 'boost' => 2,
     * ]
     * ]
     * ]
     * ],
     * ],
     * ],
     * sort' => ['id'=>['order'=>'desc']],
     * 'from' => $from,
     * 'size' => $size
     * ];
     * @return array
     */

    public function search_doc($index_name = "test_ik", $type_name = "goods", $body = [])
    {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'body' => $body,
        ];
        $response = $this->client->search($params);
        return $response;
    }


    /*
     * 高亮搜索
     * */
    public function lightSearch(string $index_name,string $where,array $fields=[]){
        $params = [
            'index' => $index_name,//索引名称
            'body'  => [
                'query' => [
                    'multi_match' => [
                        'query' => $where,//搜索条件
                        'fields' => $fields//搜索的字段
                    ],
                ],
                'highlight' => [
                    //设置高亮显示的标签
                    "pre_tags" => ["<span class='yx_hl' style='color: red'>"],
                    "post_tags" => [ "</span>"],
                    'fields' => [
                        //设置所有关键字高亮显示
                        '*' => new Highlighter()//实例化高亮插件
//                        composer require nunomaduro/collision
//                        use NunoMaduro\Collision\Highlighter;
                    ]
                ]
            ]
        ];

        $response = $this->client->search($params);
        $response=array_column($response['hits']['hits'], 'highlight');
        return $response;
//        return json(['code'=>200,'msg'=>'成功','data'=>$response]) ;
    }

?控制器循环展示:

public function light(Request $request){
    $where=$request->param('text');
    $fieds=['text'];
    $res=new Es();
    $data=$res->lightSearch('title_index',$where,$fieds);
    foreach ($data as $k=>$v){
        $data[$k]['_source']['text']=$v['text'];
    }
    return json(['code'=>200,'msg'=>'成功','data'=>$data]);

}
  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-08-16 11:32:11  更:2021-08-16 11:33:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/15 6:03:35-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码