栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 云计算 > 云平台

laravel 实现es高亮搜索

云平台 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

laravel 实现es高亮搜索

 composer安装命令   

composer require elasticsearch/elasticsearch
在app目录下创建ES目录及MyEs类
client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
    }


    
    public function exists_index($index_name = 'test_ik')
    {
        $params = [
            'index' => $index_name
        ];
        try {
            return $this->client->indices()->exists($params);
        } catch (ElasticsearchCommonExceptionsBadRequest400Exception $e) {
            $msg = $e->getMessage();
            $msg = json_decode($msg,true);
            return $msg;
        }
    }

    
    public function create_index($index_name = 'test_ik') { // 只能创建一次
        $params = [
            'index' => $index_name,
            'body' => [
                'settings' => [
                    'number_of_shards' => 5,
                    'number_of_replicas' => 1
                ]
            ]
        ];
        try {
            return $this->client->indices()->create($params);
        } catch (ElasticsearchCommonExceptionsBadRequest400Exception $e) {
            $msg = $e->getMessage();
            $msg = json_decode($msg,true);
            return $msg;
        }
    }

    
    public function delete_index($index_name = 'test_ik') {
        $params = ['index' => $index_name];
        $response = $this->client->indices()->delete($params);
        return $response;
    }

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

    
    public function exists_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];
        $response = $this->client->exists($params);
        return $response;
    }

    
    public function get_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];
        $response = $this->client->get($params);
        return $response;
    }

    
    public function update_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods', $body=[]) {
        // 可以灵活添加新字段,最好不要乱添加
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id,
            'body' => $body
        ];
        $response = $this->client->update($params);
        return $response;
    }

    
    public function delete_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];
        $response = $this->client->delete($params);
        return $response;
    }

    
    public function esSearch($data)
    {
        //获取搜索的数据
        $word = $data;
        if (empty($word)) {
//            $data = Housing::with(['HouOtt', 'HouPay', 'HouSet', 'HouType'])->get()->toArray();
            return response()->json(['code' => 1, 'msg' => 'word参数不能为空', 'data' => $word]);
        }
        //分页
//        $page = $request->get('page', 1);//接收当前页,默认值是1
//        $size = config('pagesize');//每页显示条数
//        $from = ($page - 1) * $size;//偏移量
        $params = [
            'index' => 'goods', //自己的索引名称
            'body' => [
                //执行
                'query' => [
                    //匹配
                    'match' => [
                        'title' => $word
                    ]
                ],
                'highlight' => [
                    'pre_tags' => [""],
                    'post_tags' => [''],
                    'fields' => [
                        'title' => new stdClass()
                    ]
                ]
            ],
//            'size' => $size,//每页显示的条数
//            'from' => $from//偏移量
        ];
        $res = $this->client->search($params);
        $data = $res['hits']['hits'];
        foreach ($data as $k => $v) {
            $data[$k]['_source']['title'] = $v['highlight']['title'][0];
        }
        $data = array_column($data, '_source');
        return $data;
    }

}


//调用方式(其中一种)
$es = new MyEs();
$data = $es->方法(); //注意是否需要传参


//视图高亮显示
{!! $v['title'] !!}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/896226.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号