laravel 結合ElasticSearch高亮顯示
阿新 • • 發佈:2021-01-25
技術標籤:elasticsearchlaravel
安裝
composer require elasticsearch/elasticsearch
建立索引
private $client;
public function __construct()
{
$this->client = ClientBuilder::create(['127.0.0.1:9200'])->build();
}
//建立索引
public function createindex()
{
$params = [
'index' => 'article',
'body' => [
'mappings' => [
'properties' => [
'title' => [
'type' => 'text',
'analyzer' => 'ik_max_word',
'search_analyzer' => 'ik_max_word'
],
'content'=>[
'type' => 'text',
'analyzer' => 'ik_max_word',
'search_analyzer' => 'ik_max_word'
]
] ,
],
],
];
$response = $this->client->indices()->create($params);
var_dump($response);
}
生成事件
php artisan event:generate
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class Article
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $table;
public $data;
public $id;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($table,$data,$id)
{
//
$this->table=$table;
$this->data=$data;
$this->id=$id;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
<?php
namespace App\Listeners;
use App\Events\Article;
use Elasticsearch\ClientBuilder;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class ArticleListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param Article $event
* @return void
*/
public function handle(Article $event)
{
$client = ClientBuilder::create(['127.0.0.1:9200'])->build();
$params = [
'index' => $event->table,
'type' => '_doc',
'id' => $event->id,
'body' => $event->data
];
// 新增資料到索引文件中
$response=$client->index($params);
return $response;
}
}
新增資料
public function create(Request $request)
{
if ($request->isMethod('post')){
$validator = \Validator::make($request->all(), [
'title' => 'required',
'content' => 'required',
],[
'title.required'=>'標題必填',
'content.required'=>'內容必填',
]);
if ($validator->fails()) {
return redirect('/articles/create')
->withErrors($validator)
->withInput();
}
$data=$request->post();
$id=DB::table('article')->insertGetId($data);
if($id){
//索引新增資料 事件
Event::dispatch(new Article('article',$data,$id));
return ['code'=>200,'msg'=>'新增成功','data'=>$data];
}
return ['code'=>400,'msg'=>'失敗','result'=>null];
}
return view('article/create');
}
搜尋高亮顯示
//搜尋
public function index(Request $request){
$searchKey=$request->get('searchKey');
if ($searchKey){
$params = [
'index' => 'article',
'body' => [
'query' => [
'multi_match' => [
"query"=> $searchKey,
"fields"=> [ "title", "content" ],
]
],
'highlight' => array(
"pre_tags"=> ["<span style='color:red'>"],
"post_tags"=>["</span>"],
'fields' => array(
'*' => new Highlighter(),
)
)
]
];
$response = $this->client->search($params);
if ($response['hits']['total']['value'] > 0 ){
foreach ($response['hits']['hits'] as $v){
$data=$v['highlight'];
}
}
return ['code'=>200,'msg'=>'獲取成功','data'=>$data];
}else{
$arr= DB::table('article')->get();
return ['code'=>200,'msg'=>'獲取成功','data'=>$arr];
}