PHP+laravel之ES搜索

PHP+laravel之ES搜索,第1张

PHP+laravel之ES搜索

1. 检查电脑有没有Java SE环境(CMD中输入java -version),如果没有的话,没有的话请移步Java Archive | Oracle 下载JAVA SE,下载完成之后配置环境变量。不会配环境的移步

https://note.youdao.com/ynoteshare/index.html?id=5d588e37d757a264e269cba4e694a697&type=note&_time=1637629414584

2.下载ES与Kibana  移步 下载 Elastic 产品 | Elastic

3.下载ik分词器  移步 https://github.com/medcl/elasticsearch-analysis-ik/releases

4.ES  KIBANA  IK 分词器下载的版本要一致   如 ES使用7.1.0  kibana也要7.1.0  ik分词也要7.1.0

5.下载完IK分词器后,解压到这个目录下并将文件夹名改为ik

6.启动ES 与 KIBANA 

启动ES   

 启动KIBANA  

启动完之后,到浏览器检查是否启动成功 ES:(http://localhost:9200)KIBANA(http://localhost:5601),例 ES启动成功

例  KIBANA启动成功:

7. 框架内执行composer require elasticsearch/elasticsearch 安装扩展。

各种准备工作结束,接下来上代码

1. 创建索引

  public function createIndex(){
        // 创建索引
        $client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
        $params = [
            'index' => 'news',
            'body' => [
                'settings' => [
                    'number_of_shards' => 3,
                    'number_of_replicas' => 2
                ],
                'mappings' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        //  ‘title’ 字段
                        'title' => [
                            'type' => 'text',
                            "analyzer" => "ik_max_word",
                            "search_analyzer" => "ik_max_word"
                        ],
                        'author' => [
                            'type' => 'text',
                            "analyzer" => "ik_max_word",
                            "search_analyzer" => "ik_max_word"
                        ]
                    ]
                ]
            ]
        ];
        // Create the index with mappings and settings now
        $response = $client->indices()->create($params);
        return $response;
    }

2. 第二步,将数据同步到ES  (这里看你的逻辑是什么)

 //封装一个同步的方法,到时候直接调用该方法
public function  pushEsNews($data){
        $client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
            $params = [
                'index' => 'news',
                'type' => '_doc',
                'body' => $data
            ];
            $response = $client->index($params);
        return $response;
    }
 //我这边用的采集,你可以换成正常的添加数据。
public function queryList(){
        //数据采集
        $ql =  QueryList::get("http://www.rz-tea.com/");
        $data['author'] =  $ql->find('.topic a')->texts();
        $data['title'] =  $ql->find('.article-title a')->texts();
        $data['src'] =  $ql->find('.article-image img')->attrs('src');
        $data['issue_date'] =  $ql->find('.article-meta .publish')->texts();
        $data['intro'] =  $ql->find('.article-digest')->texts();
        $data['info_src'] =  $ql->find('.article-image a')->attrs('href');
        foreach ($data['author'] as $key=>$val){
            unset($newData);
            $newData['author'] = $val;
            $newData['title'] = $data['title'][$key];
            $newData['src'] = $data['src'][$key];
            $newData['issue_date'] = $data['issue_date'][$key];
            $newData['info_src'] = $data['info_src'][$key];
            $newData['intro'] = $data['intro'][$key];
            $id  =  News::insertGetId($newData);
            //将数据同步到ES
            $newData['id'] = $id;
            $this->pushEsNews($newData);
        }
    }

3.搜索后高亮显示

  public function show(request $request){
        //搜索的值
        $seach = $request['seach'];
        $res = News::get()->toArray();
        //判断用户是否搜索,如果没有则跳过
        if($seach!=""){
            $client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
            $params = [
                'index' => 'news',
                'type' => '_doc',
                'body' => [
                    'query' => [
                        'bool' => [
                            'should' => [
                                [
                                    'match' => [
                                        'title' => "$seach",
                                    ]
                                ]
                            ]
                        ]
                    ],
                    'highlight' => [
                        'pre_tags' => [""],
                        'post_tags' => [""],
                        'fields' => [
                            "title" => new stdClass()
                        ]
                    ]
                ]
            ];
            $response = $client->search($params);
            $data = $response['hits']['hits'];
            $res = [];
            foreach ($data as $v) {
                if (!empty($v['highlight']['title'][0])) {
                    $v['_source']['title'] = $v['highlight']['title'][0];
                }
                array_push($res, $v['_source']);
            }
        }
        return view('news_show',compact('res','seach'));
    }

4.前台展示HTML

    
      批量删除  添加管理员 共有数据:54 条 
    
        
        
            员工列表
        
        
            
            ID
            发布作者
            新闻标题
            新闻简介
            新闻封面
            发布时间
            是否启用
             *** 作
        
        
        
        @foreach($res as $key=>$val)
            
                
                {{$val['id']}}
                {{$val['author']}}
                {!!$val['title']!!}
                {{$val['intro']}}
                {$val['src']}}" alt="" width="100%" height="100%">
                {{$val['issue_date']}}
                已启用
                  
            
        @endforeach
        
    

最终效果:

结束!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5606808.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存