文章目录




在做这个功能前,你需要安装好你的elasticSearch

参考文档:https://cloud.tencent.com/developer/article/1350622

注册elasticSearch服务

【ElasticSearch】结合laravel做高亮搜索_搜索

配置

【ElasticSearch】结合laravel做高亮搜索_ElasticSearch_02

env配置

【ElasticSearch】结合laravel做高亮搜索_参考文档_03

在控制器创建搜索

【ElasticSearch】结合laravel做高亮搜索_参考文档_04

public function search()
{
$params = [
'index' => 'goods',
'type' => '_doc',
'body' => [
'query' => [
'match' => [
'long_title' => '随便'
],
],
"highlight" => [
"fields" => [
"long_title" => [
"pre_tags" => [
"<span style='color: red'>"
],
"post_tags" => [
"</span>"
]
]
]
],
// "size"=>10000,
],
];

$data = app('es')->search($params);
// dump($data);die;
// dump($data['hits']['hits']);die;
foreach($data['hits']['hits'] as $k => $v){
// dump($data['hits']['hits'][$k]['_source']['long_title']);die;
$data['hits']['hits'][$k]['_source']['long_title'] = $v['highlight']['long_title'][0];
}
$realData = $data['hits']['hits'];
return view('web.member.search',[
'realData' => $realData
]);
}

设置视图

【ElasticSearch】结合laravel做高亮搜索_ElasticSearch_05

展示效果

【ElasticSearch】结合laravel做高亮搜索_ElasticSearch_06