**一、安裝**
通過composer安裝
~~~
composer require 'elasticsearch/elasticsearch'
~~~
##
## **二、使用**
創建ES類
~~~
<?php
require 'vendor/autoload.php';
//如果未設置密碼
$es = \Elasticsearch\ClientBuilder::create()->setHosts(['xxx.xxx.xxx.xxx'])->build();
//如果es設置了密碼
$es = \Elasticsearch\ClientBuilder::create()->setHosts(['http://username:password@xxx.xxx.xxx.xxx:9200'])->build()
~~~
## **三、新建ES數據庫**
index 對應關系型數據(以下簡稱MySQL)里面的數據庫,而不是對應MySQL里面的索引
~~~
<?php
$params = [
'index' => 'autofelix_db', #index的名字不能是大寫和下劃線開頭
'body' => [
'settings' => [
'number_of_shards' => 5,
'number_of_replicas' => 0
]
]
];
$es->indices()->create($params);
~~~
## **四、創建表**
在MySQL里面,光有了數據庫還不行,還需要建立表,ES也是一樣的
ES中的type對應MySQL里面的表
ES6以前,一個index有多個type,就像MySQL中一個數據庫有多個表一樣
但是ES6以后,每個index只允許一個type
在定義字段的時候,可以看出每個字段可以定義單獨的類型
在first_name中還自定義了 分詞器 ik,這是個插件,是需要單獨安裝的
~~~
<?php
$params = [
'index' => 'autofelix_db',
'type' => 'autofelix_table',
'body' => [
'mytype' => [
'_source' => [
'enabled' => true
],
'properties' => [
'id' => [
'type' => 'integer'
],
'first_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'last_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'age' => [
'type' => 'integer'
]
]
]
]
];
$es->indices()->putMapping($params);
~~~
~~~
五、插入數據
現在數據庫和表都有了,可以往里面插入數據了
在ES里面的數據叫文檔
可以多插入一些數據,等會可以模擬搜索功能
<?php
$params = [
'index' => 'autofelix_db',
'type' => 'autofelix_table',
//'id' => 1, #可以手動指定id,也可以不指定隨機生成
'body' => [
'first_name' => '飛',
'last_name' => '兔',
'age' => 26
]
];
$es->index($params);
六、 查詢所有數據
<?php
$data = $es->search();
var_dump($data);
七、查詢單條數據
如果你在插入數據的時候指定了id,就可以查詢的時候加上id
如果你在插入的時候未指定id,系統將會自動生成id,你可以通過查詢所有數據后查看其id
<?php
$params = [
'index' => 'autofelix_db',
'type' => 'autofelix_table',
'id' => //你插入數據時候的id
];
$data = $es->get($params);
八、搜索
ES精髓的地方就在于搜索
<?php
$params = [
'index' => 'autofelix_db',
'type' => 'autofelix_table',
'body' => [
'query' => [
'constant_score' => [ //非評分模式執行
'filter' => [ //過濾器,不會計算相關度,速度快
'term' => [ //精確查找,不支持多個條件
'first_name' => '飛'
]
]
]
]
]
];
$data = $es->search($params);
var_dump($data);
九、測試代碼
基于Laravel環境,包含刪除數據庫,刪除文檔等操作
<?php
use Elasticsearch\ClientBuilder;
use Faker\Generator as Faker;
/**
* ES 的 php 實測代碼
*/
class EsDemo
{
private $EsClient = null;
private $faker = null;
/**
* 為了簡化測試,本測試默認只操作一個Index,一個Type
*/
private $index = 'autofelix_db';
private $type = 'autofelix_table';
public function __construct(Faker $faker)
{
/**
* 實例化 ES 客戶端
*/
$this->EsClient = ClientBuilder::create()->setHosts(['xxx.xxx.xxx.xxx'])->build();
/**
* 這是一個數據生成庫
*/
$this->faker = $faker;
}
/**
* 批量生成文檔
* @param $num
*/
public function generateDoc($num = 100) {
foreach (range(1,$num) as $item) {
$this->putDoc([
'first_name' => $this->faker->name,
'last_name' => $this->faker->name,
'age' => $this->faker->numberBetween(20,80)
]);
}
}
/**
* 刪除一個文檔
* @param $id
* @return array
*/
public function delDoc($id) {
$params = [
'index' => $this->index,
'type' => $this->type,
'id' =>$id
];
return $this->EsClient->delete($params);
}
/**
* 搜索文檔,query是查詢條件
* @param array $query
* @param int $from
* @param int $size
* @return array
*/
public function search($query = [], $from = 0, $size = 5) {
// $query = [
// 'query' => [
// 'bool' => [
// 'must' => [
// 'match' => [
// 'first_name' => 'Cronin',
// ]
// ],
// 'filter' => [
// 'range' => [
// 'age' => ['gt' => 76]
// ]
// ]
// ]
//
// ]
// ];
$params = [
'index' => $this->index,
// 'index' => 'm*', #index 和 type 是可以模糊匹配的,甚至這兩個參數都是可選的
'type' => $this->type,
'_source' => ['first_name','age'], // 請求指定的字段
'body' => array_merge([
'from' => $from,
'size' => $size
],$query)
];
return $this->EsClient->search($params);
}
/**
* 一次獲取多個文檔
* @param $ids
* @return array
*/
public function getDocs($ids) {
$params = [
'index' => $this->index,
'type' => $this->type,
'body' => ['ids' => $ids]
];
return $this->EsClient->mget($params);
}
/**
* 獲取單個文檔
* @param $id
* @return array
*/
public function getDoc($id) {
$params = [
'index' => $this->index,
'type' => $this->type,
'id' =>$id
];
return $this->EsClient->get($params);
}
/**
* 更新一個文檔
* @param $id
* @return array
*/
public function updateDoc($id) {
$params = [
'index' => $this->index,
'type' => $this->type,
'id' =>$id,
'body' => [
'doc' => [
'first_name' => '張',
'last_name' => '三',
'age' => 99
]
]
];
return $this->EsClient->update($params);
}
/**
* 添加一個文檔到 Index 的Type中
* @param array $body
* @return void
*/
public function putDoc($body = []) {
$params = [
'index' => $this->index,
'type' => $this->type,
// 'id' => 1, #可以手動指定id,也可以不指定隨機生成
'body' => $body
];
$this->EsClient->index($params);
}
/**
* 刪除所有的 Index
*/
public function delAllIndex() {
$indexList = $this->esStatus()['indices'];
foreach ($indexList as $item => $index) {
$this->delIndex();
}
}
/**
* 獲取 ES 的狀態信息,包括index 列表
* @return array
*/
public function esStatus() {
return $this->EsClient->indices()->stats();
}
/**
* 創建一個索引 Index (非關系型數據庫里面那個索引,而是關系型數據里面的數據庫的意思)
* @return void
*/
public function createIndex() {
$this->delIndex();
$params = [
'index' => $this->index,
'body' => [
'settings' => [
'number_of_shards' => 2,
'number_of_replicas' => 0
]
]
];
$this->EsClient->indices()->create($params);
}
/**
* 檢查Index 是否存在
* @return bool
*/
public function checkIndexExists() {
$params = [
'index' => $this->index
];
return $this->EsClient->indices()->exists($params);
}
/**
* 刪除一個Index
* @return void
*/
public function delIndex() {
$params = [
'index' => $this->index
];
if ($this->checkIndexExists()) {
$this->EsClient->indices()->delete($params);
}
}
/**
* 獲取Index的文檔模板信息
* @return array
*/
public function getMapping() {
$params = [
'index' => $this->index
];
return $this->EsClient->indices()->getMapping($params);
}
/**
* 創建文檔模板
* @return void
*/
public function createMapping() {
$this->createIndex();
$params = [
'index' => $this->index,
'type' => $this->type,
'body' => [
$this->type => [
'_source' => [
'enabled' => true
],
'properties' => [
'id' => [
'type' => 'integer'
],
'first_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'last_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'age' => [
'type' => 'integer'
]
]
]
]
];
$this->EsClient->indices()->putMapping($params);
$this->generateDoc();
}
}
~~~
- 文檔說明
- 開始
- linux
- 常用命令
- ps -ef
- lsof
- netstat
- 解壓縮
- 復制
- 權限
- 其他
- lnmp集成安裝
- supervisor
- 安裝
- supervisor進程管理
- nginx
- 域名映射
- 負載均衡配置
- lnmp集成環境安裝
- nginx源碼安裝
- location匹配
- 限流配置
- 日志配置
- 重定向配置
- 壓縮策略
- nginx 正/反向代理
- HTTPS配置
- mysql
- navicat創建索引
- 設置外網鏈接mysql
- navicat破解
- sql語句學習
- 新建mysql用戶并賦予權限
- php
- opcache
- 設計模式
- 在CentOS下安裝crontab服務
- composer
- 基礎
- 常用的包
- guzzle
- 二維碼
- 公共方法
- 敏感詞過濾
- IP訪問頻次限制
- CURL
- 支付
- 常用遞歸
- 數據排序
- 圖片相關操作
- 權重分配
- 毫秒時間戳
- base64<=>圖片
- 身份證號分析
- 手機號相關操作
- 項目搭建 公共處理函數
- JWT
- 系統函數
- json_encode / json_decode 相關
- 數字計算
- 數組排序
- php8
- jit特性
- php8源碼編譯安裝
- laravel框架
- 常用artisan命令
- 常用查詢
- 模型關聯
- 創建公共方法
- 圖片上傳
- 中間件
- 路由配置
- jwt
- 隊列
- 定時任務
- 日志模塊
- laravel+swoole基本使用
- 拓展庫
- 請求接口log
- laravel_octane
- 微信開發
- token配置驗證
- easywechart 獲取用戶信息
- 三方包
- webman
- win下熱更新代碼
- 使用laravel db listen 監聽sql語句
- guzzle
- 使用workman的httpCLient
- 修改隊列后代碼不生效
- workman
- 安裝與使用
- websocket
- eleticsearch
- php-es 安裝配置
- hyperf
- 熱更新
- 安裝報錯
- swoole
- 安裝
- win安裝swoole-cli
- google登錄
- golang
- 文檔地址
- 標準庫
- time
- 數據類型
- 基本數據類型
- 復合數據類型
- 協程&管道
- 協程基本使用
- 讀寫鎖 RWMutex
- 互斥鎖Mutex
- 管道的基本使用
- 管道select多路復用
- 協程加管道
- beego
- gin
- 安裝
- 熱更新
- 路由
- 中間件
- 控制器
- 模型
- 配置文件/conf
- gorm
- 初始化
- 控制器 模型查詢封裝
- 添加
- 修改
- 刪除
- 聯表查詢
- 環境搭建
- Windows
- linux
- 全局異常捕捉
- javascript
- 常用函數
- vue
- vue-cli
- 生產環境 開發環境配置
- 組件通信
- 組件之間通信
- 父傳子
- 子傳父
- provide->inject (非父子)
- 引用元素和組件
- vue-原始寫法
- template基本用法
- vue3+ts項目搭建
- vue3引入element-plus
- axios 封裝網絡請求
- computed 計算屬性
- watch 監聽
- 使用@符 代替文件引入路徑
- vue開發中常用的插件
- vue 富文本編輯
- nuxt
- 學習筆記
- 新建項目踩坑整理
- css
- flex布局
- flex PC端基本布局
- flex 移動端基本布局
- 常用css屬性
- 盒子模型與定位
- 小說分屏顯示
- git
- 基本命令
- fetch
- 常用命令
- 每次都需要驗證
- git pull 有沖突時
- .gitignore 修改后不生效
- 原理解析
- tcp與udp詳解
- TCP三次握手四次揮手
- 緩存雪崩 穿透 更新詳解
- 內存泄漏-內存溢出
- php_fpm fast_cgi cig
- redis
- 相關三方文章
- API對外接口文檔示范
- elaticsearch
- 全文檢索
- 簡介
- 安裝
- kibana
- 核心概念 索引 映射 文檔
- 高級查詢 Query DSL
- 索引原理
- 分詞器
- 過濾查詢
- 聚合查詢
- 整合應用
- 集群
- docker
- docker 簡介
- docker 安裝
- docker 常用命令
- image 鏡像命令
- Contrainer 容器命令
- docker-compose
- redis 相關
- 客戶端安裝
- Linux 環境下安裝
- uni
- http請求封裝
- ios打包
- 視頻縱向播放
- 日記
- 工作日記
- 情感日志
- 壓測
- ab
- ui
- thorui
- 開發規范
- 前端
- 后端
- 狀態碼
- 開發小組未來規劃