栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ES高亮搜索简单实现

ES高亮搜索简单实现

composer require elasticsearch/elasticsearch

es入口

namespace AppHttpEs;

use ElasticsearchClientBuilder;

class EsService
{
  
    private static $EsClient = false;
   

    private function __construct()
    {

    }

    public static function getIntance()
    {
        if (self::$EsClient === false) {
            self::$EsClient = ClientBuilder::create()->build();
        }
        return self::$EsClient;
    }
}
 //
use ElasticsearchClientBuilder;
use AppHttpEsEsService;



    public function esCreate()
    {
        $client = ClientBuilder::create()->build();
        $params = [
            'index' => 'area',//自定义index(库名)
            'body' => [
                'settings' => [
                    'number_of_shards' => 3,
                    'number_of_replicas' => 2
                ],
                'mappings' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        'area' => [
                            'type' => 'text',
                            "analyzer" => "ik_max_word",
                            "search_analyzer" => "ik_max_word"
                        ]
                    ]
                ]
            ]
        ];
        //执行创建
        $res = $client->indices()->create($params);
        dd($res);
    }

    public function esAdd()
    {
        $data = Shop::all()->toArray();//获取到数据
        $client =EsController::getIntance();
        foreach ($data as $k=>$v){
            $params = [
                'index' =>'area',//上面的自定义index
                'type'  =>'_doc',//表(固定的写法)
                'id'    =>  $v['id'],//主键
                'body'     =>$v//数据
            ];

            $res = $client->index($params);
        }

        dd($res);

    }

    public function esSearch(Request $request)
    {
        $search_field = 'area';
        $word =$request->get('word');//接收关键字
        $client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();//创建es实例
        //设置查询的条件
        if (empty($word)){
            $body=[

            ];
        }else{
            $body=[
                //查询内容
                'query' => [
                    'match' => [//匹配
                        $search_field => $word//匹配字段
                    ]
                ],
                'highlight' => [//高亮
                    'pre_tags' => [""],//样式自己写
                    'post_tags' => [""],
                    'fields' => [
                        $search_field => new stdClass()
                    ]
                ]
            ];
        }
        $params = [
            'index' => 'area',//自定义的index
            'body' => $body,
        ];
        $results = $client->search($params);//es搜索
        if (!empty($word)){
            foreach ($results['hits']['hits'] as $k=>$v){
                $results['hits']['hits'][$k]['_source'][$search_field] = $v['highlight'][$search_field][0];
            }
        }
        $data = array_column($results['hits']['hits'],'_source');

        $data = array_column($results['hits']['hits'],'_source');

        return response()->json(['code'=>200,'msg'=>'ok','data'=>$data]);

    }

详见:php elasticsearch操作_通财的博客-CSDN博客

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

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

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