`composer require elasticsearch/elasticsearch 7.15`
#### 操作類
`<?php
namespace app\index;
use Elasticsearch\ClientBuilder;
class EsService
{
private $client;
public function __construct()
{
$params = [
'host' => '127.0.0.1',
'port' => '9200',
// 'scheme' => 'http',
'user' => 'es',
'pass' => 'es'
];
$this->client = ClientBuilder::create()->setHosts($params)->setConnectionPool('\Elasticsearch\ConnectionPool\SimpleConnectionPool', [])
->setRetries(10)->build();
}
/**
* @param string $index_name
* @return array
* 創建索引
*/
public function create_index(string $index_name = 'es_test')
{ // 只能創建一次哦
$params = [
'index' => $index_name,
'body' => [
'settings' => [
'number_of_shards' => 15,
'number_of_replicas' => 1
]
],
];
return $this->client->indices()->create($params);
}
/**
* @param string $index_name
* @return array
* 刪除索引
*/
public function delete_index(string $index_name = 'es_test')
{
$params = ['index' => $index_name];
$response = $this->client->indices()->delete($params);
return $response;
}
/**
* @param string $type_name
* @param string $index_name
* @return array
* 創建文檔模板
*/
public function create_mapping(string $type_name = 'es_test_type', string $index_name = 'es_test')
{
$params = [
'index' => $index_name,//這里是索引名,相當于數據庫名
'type' => $type_name,//這里是類型名,相當于表名
'include_type_name' => true,
'body' => [
//下面是數據類型定義,相當于數據庫字段
'properties' => [
'id' => [
'type' => 'byte', // 整型
'index' => 'false', // 非全文搜索
],
'name' => [
'type' => 'text', // 字符串型
'index' => 'true', // 全文搜索
],
'desc' => [
'type' => 'text', // 字符串型
'index' => 'true', // 全文搜索
],
'age' => [
'type' => 'integer',
'index' => 'false', //非 全文搜索
],
]
]
];
return $this->client->indices()->putMapping($params);
}
/**
* @param string $keywords
* @param string $index_name
* @param string $type_name
* @param int $from
* @param int $size
* @return array|callable
* 查詢文檔 (分頁,排序,權重,過濾)
*/
public function search_doc(string $keywords = "跑啊", string $index_name = "es_test", $type_name = "es_test_type", $from = 0, $size = 10)
{
$params = [
'index' => $index_name,
'type' => $type_name,
'body' => [
'query' => [
'bool' => [
//bool查詢,可以把很多小的查詢組成一個更為復雜的查詢,
'should' => [
// 這里should是查詢desc字段包含$keywords關鍵詞或者name字段包含$keywords關鍵詞的文檔。
//可以改為"must"意思是同時包含。must_not排除包含
['match' =>
[
'desc' =>
[
'query' => $keywords,
'boost' => 3, // 權重大
]
]
],
['match' =>
['name' =>
[
'query' => $keywords,
'boost' => 2,
]
]
],
],
],
],
'sort' =>
[
'age' => ['order' => 'desc']
]
, 'from' => $from, 'size' => $size
]
];
return $this->client->search($params);
}
/**
* @param string $type_name
* @param string $index_name
* @return array
* 查看映射
*/
public function get_mapping(string $type_name = 'es_test_type', string $index_name = 'es_test')
{
$params = [
'index' => $index_name,
'type' => $type_name,
'include_type_name' => true,
];
return $this->client->indices()->getMapping($params);
}
/**
* @param $id
* @param array $doc
* @param string $index_name
* @param string $type_name
* @return array|callable
* 添加單個文檔
*/
public function add_single_doc($id, array $doc, string $index_name = 'es_test', string $type_name = 'es_test_type')
{
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id,
'body' => $doc
];
return $this->client->index($params);
}
/**
* @param $body
* @param string $index_name
* @param string $type_name
* @return array
* 批量添加文檔
*/
public function add_bulk_doc(array $body, string $index_name = 'es_test', string $type_name = 'es_test_type')
{
$params = [
'index' => $index_name,
'type' => $type_name,
'body' => $body
];
return $this->client->bulk($params);
}
/**
* @param $id
* @param string $index_name
* @param string $type_name
* @return bool
* 判斷文檔存在
*/
public function exists_doc($id, string $index_name = 'es_test', string $type_name = 'es_test_type')
{
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
return $this->client->exists($params);
}
/**
* @param $id
* @param string $index_name
* @param string $type_name
* @return array|callable
* 獲取文檔
*/
public function get_doc($id, string $index_name = 'es_test', string $type_name = 'es_test_type')
{
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
return $this->client->get($params);
}
/**
* @param $id
* @param array $arr
* @param string $index_name
* @param string $type_name
* @return array|callable
* 更新文檔
*/
public function update_doc($id, array $arr, string $index_name = 'es_test', string $type_name = 'es_test_type')
{
// 可以靈活添加新字段,最好不要亂添加
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id,
'body' => [
'doc' => $arr //['name'=>'ssss','age'=>56]
]
];
return $this->client->update($params);
}
/**
* @param $id
* @param string $index_name
* @param string $type_name
* @return array|callable
* 刪除文檔
*/
public function delete_doc($id, string $index_name = 'es_test', string $type_name = 'es_test_type')
{
$params = [
'index' => $index_name,
'type' => $type_name,
'id' => $id
];
return $this->client->delete($params);
}
protected function __clone()
{
// TODO: Implement __call() method.
}
}
`
#### 使用
` public function index()
{
$EsService=new EsService();
//創建索引
$EsService->create_index();
//創建文檔模板
$EsService->create_mapping();
//單個添加
$data = [];
$data[] = ['id' => 1, 'name' => 'php', 'desc' => '世界上最強的語言。', 'age' => 23];
$data[] = ['id' => 2, 'name' => 'laravel', 'desc' => '優雅的框架。', 'age' => 24];
$data[] = ['id' => 3, 'name' => 'java', 'desc' => '牛逼后端語言', 'age' => 29];
foreach ($data as $k=>$v){
//添加文檔
$EsService->add_single_doc($v['id'],$v);
}
//批量添加
$arr=[];
for ($i=0;$i<100;$i++){
$arr[]=[
'id'=>$i+1,
'name'=>'name_'.mt_rand(1,9222),
'desc'=>'desc_'.mt_rand(8787,922102),
'age'=>mt_rand(8,100)
];
}
$body=[];
foreach ($arr as $k=>$v){
$body[($k*2)] = ['index' => [ '_id' => $v['id'].'_'.$v['age'],'_type'=>'es_test_type','_index'=>'es_test']];
$body[(($k*2)+1)] = $v;
}
$EsService->add_bulk_doc($body);
//搜索
$result=$EsService->search_doc('php');
dump($result);
}
`
- 空白目錄
- thinkphp5
- tools-常用類庫
- redis類庫
- Excel類庫
- File文件操作類庫
- Http請求類庫
- Maile郵件發送
- Hooks行為鉤子
- 七牛云
- 隨機數和字符串生成
- 字符串處理
- 時間類處理
- tree型轉換
- 工具類庫
- 文件打包下載
- 常用功能
- 文件上傳
- php生成word文檔
- elasticsearch 基本搜索
- 使用jwt開發API接口
- 安裝模及搭建
- ApiCheck.php
- ApiCheckLogin.php
- common.php
- Login.php
- Comment.php
- 漢字轉拼音
- 安裝使用
- Pinyin類
- elasticsearch操作
- 常用方法
- 數據源生成layui-select
- 獲取自定義配置項
- 百度編輯器
- 格式化文件大小
- 多語言設置
- hook監聽
- 域名綁定到模塊
- thinkphp6
- 文件上傳
- tp5totp6
- 創建路徑
- 獲取類所有方法
- password_hash加密驗證
- 生成 qrcode
- 郵件發送
- 獲取QQ信息
- GoogleAuthenticator
- redis限流
- redis 加鎖
- 百度翻譯
- QueryList爬取數據
- 獲取時間類
- 命令
- Git常用命令
- easyswoole
- pix_qrcode
- 驗證 cpf,cnpj
- php常用方法
- 日志
- 卡通頭像
- 兩位小數
- 圖片轉base64
- auth加密解密
- phpoffice/phpspreadsheet導入導出
- fastadmin
- 樹結構
- 單選框
- 復選框
- 二級搜索
- select選擇框
- selectpage選中回調事件
- 標簽添加
- 修改where條件
- 表格列表中添加input框
- selectpage事件
- fieldlist
- js操作
- test_js
- 多表格
- template模板