public function __construct()
{
$this->client=ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
}
public function es($index,$type,$id,$body)
{
$params=[
'index'=>$index,
'type'=>$type,
'id'=>$id,
'body'=>$body
];
$res=$this->client->index($params);
return $res;
}
public function ess($index,$type,$body)
{
$params = [
'index'=>$index,
'type'=>$type,
'body'=>[
'query'=>[
'match'=>[
'title'=>$body
]
],
'highlight'=>[
'pre_tags'=>[""],
'post_tags'=>[""],
'fields'=>[
'title'=> new \stdClass()
]
]
]
];
$res=$this->client->search($params);
return $res;
}
添加
在服务层封装文章来源:https://www.toymoban.com/news/detail-546559.html
composer require elasticsearch/elasticsearch文章来源地址https://www.toymoban.com/news/detail-546559.html
添加
$data = Request()->get('title');
if ($data) {
$dats = new es();
$sy = $dats->ess('', '', $data);
$score = $sy['hits']['hits'];
foreach ($score as $k => $v) {
$score[$k]['_source']['title'] = $v['highlight']['title']['0'];
}
$datas = array_column($score, '_source');
return json_encode(['code' => 200, 'meg' => '成功', 'data' => $datas]);
}
高亮搜索
$data = Request()->all();
$dats = goods::create($data);
$mk=[
'title'=>$dats->title,
'neirong'=>$dats->neirong,
'uname'=>$dats->uname
];
$es= new es();
$es->es('','',$dats->id,$mk);
return json_encode(['code' => 200, 'meg' => '成功', 'data' => $dats]);
到了这里,关于PHP es 高亮显示的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!