文章目录
在做这个功能前,你需要安装好你的elasticSearch
参考文档:https://cloud.tencent.com/developer/article/1350622
注册elasticSearch服务
配置
env配置
在控制器创建搜索
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
]);
}
设置视图
展示效果