composer安裝
composer require elasticsearch/elasticsearch
ES封裝
<?php
namespace App\Es;
use Elasticsearch\ClientBuilder;
class MyEs
{
//ES客戶端連結
private $client;
/**
* 建構函式
* MyElasticsearch constructor.
*/
public function __construct()
{
$this->client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
}
/**
* 判斷索引是否存在
* @param string $index_name
* @return bool|mixed|string
*/
public function exists_index($index_name = 'test_ik')
{
$params = [
'index' => $index_name
];
try {
return $this->client->indices()->exists($params);
} catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
$msg = $e->getMessage();
$msg = json_decode($msg,true);
return $msg;
}
}
/**
* 建立索引
* @param string $index_name
* @return array|mixed|string
*/
public function create_index($index_name = 'test_ik') { // 只能建立一次
$params = [
'index' => $index_name,
'body' => [
'settings' => [
'number_of_shards' => 5,
'number_of_replicas' => 1
]
]
];
try {
return $this->client->indices()->create($params);
} catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {
$msg = $e->getMessage();
$msg = json_decode($msg,true);
return $msg;
}
}
/**
* 刪除索引
* @param string $index_name
* @return array
*/
public function delete_index($index_name = 'test_ik') {
$params = ['index' => $index_name];
$response = $this->client->indices()->delete($params);
return $response;
}
/**
* 新增文件
* @param $id
* @param $doc ['id'=>100, 'title'=>'phone']
* @param string $index_name
* @param string $type_name
* @return array
*/
public function add_doc($id,$doc,$index_name = 'test_ik',$type_name = 'goods') {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id,
'body' => $doc
];
$response = $this->client->index($params);
return $response;
}
/**
* 判斷文件存在
* @param int $id
* @param string $index_name
* @param string $type_name
* @return array|bool
*/
public function exists_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
$response = $this->client->exists($params);
return $response;
}
/**
* 獲取文件
* @param int $id
* @param string $index_name
* @param string $type_name
* @return array
*/
public function get_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
$response = $this->client->get($params);
return $response;
}
/**
* 更新文件
* @param int $id
* @param string $index_name
* @param string $type_name
* @param array $body ['doc' => ['title' => '蘋果手機iPhoneX']]
* @return array
*/
public function update_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods', $body=[]) {
// 可以靈活新增新欄位,最好不要亂新增
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id,
'body' => $body
];
$response = $this->client->update($params);
return $response;
}
/**
* 刪除文件
* @param int $id
* @param string $index_name
* @param string $type_name
* @return array
*/
public function delete_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') {
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
$response = $this->client->delete($params);
return $response;
}
/**
* 搜尋文件 (分頁,排序,權重,過濾)
* @param string $index_name
* @param string $type_name
* @param array $body
* $body = [
'query' => [
'match' => [
'fang_name' => [
'query' => $fangName
]
]
],
'highlight'=>[
'fields'=>[
'fang_name'=>[
'pre_tags'=>[
'<span style="color: red">'
],
'post_tags'=>[
'</span>'
]
]
]
]
];
* @return array
*/
public function search_doc($index_name = "test_ik",$type_name = "goods",$body=[]) {
$params = [
'index' => $index_name,
'type' => $type_name,
'body' => $body
];
$results = $this->client->search($params);
return $results;
}
}
將資料表中所有資料新增至ES
public function esAdd()
{
$data = Good::get()->toArray();
$es = new MyEs();
if (!$es->exists_index('goods')) {
//建立es索引,es的索引相當於MySQL的資料庫
$es->create_index('goods');
}
foreach ($data as $model) {
$es->add_doc($model['id'], $model, 'goods', '_doc');
}
}
每在MySQL裡新增一條資料,在es裡也新增一條
直接將程式碼補在MySQL新增入庫的邏輯方法裡即可
//新增至MySQL
$res=Good::insertGetId($arr);
$es = new MyEs();
if (!$es->exists_index('goods')) {
$es->create_index('goods');
}
//新增至es
$es->add_doc($res, $arr, 'goods', '_doc');
return $res;
進行MySQL資料修改時,也更新es的資料
直接將程式碼補在MySQL修改資料的邏輯方法裡即可
//修改MySQL的資料
$res=Good::where('id',$id)->update($arr);
$es = new MyEs();
if (!$es->exists_index('goods')) {
$es->create_index('goods');
}
//修改es的資料
$es->update_doc($id, 'goods', '_doc',['doc'=>$arr]);
return $res;
通過ES實現搜尋功能
public function search()
{
//獲取搜尋值
$search = \request()->get('search');
if (!empty($search)) {
$es = new MyEs();
$body = [
'query' => [
'match' => [
'title' => [
'query' => $search
]
]
],
'highlight'=>[
'fields'=>[
'title'=>[
'pre_tags'=>[
'<span style="color: red">'
],
'post_tags'=>[
'</span>'
]
]
]
]
];
$res = $es->search_doc('goods', '_doc', $body);
$data = array_column($res['hits']['hits'], '_source');
foreach ($data as $key=>&$v){
$v['title'] = $res['hits']['hits'][$key]['highlight']['title'][0];
}
unset($v);
return $data;
}
$data = Good::get();
return $data;
}
另,補充es分頁搜尋
如果是在微信小程式中使用的話,運用上拉觸底事件即可
此功能是在上面搜尋功能之上新增程式碼實現的
1.接收前臺小程式傳遞來的當前頁
2.呼叫es封裝類的搜尋方法時,多傳兩個引數
3.在es封裝類的搜尋方法中增加兩個形參
搜尋後搜尋值高亮顯示
如果是在微信小程式中使用的話,是直接將標籤和值一起輸出到頁面的,加入解析富文字的標籤可以將標籤轉化格式,達到高亮效果
<rich-text nodes="{{item.title}}"></rich-text>
本作品採用《CC 協議》,轉載必須註明作者和本文連結