目錄
一、實戰(zhàn)場景
二、知識點(diǎn)
PHP
Elasticsearch
索引 index
MySQL
三、菜鳥實戰(zhàn)
一、實戰(zhàn)場景
如何在 PHP 中使用 Elasticsearch 的索引 API 接口
二、知識點(diǎn)
-
PHP
-
Elasticsearch
-
索引 index
-
MySQL
Elasticsearch 本質(zhì)上是一個數(shù)據(jù)庫,但并不是 MySQL 這種關(guān)系型數(shù)據(jù)庫,查詢語言也不是 SQL,而是 Elasticsearch 自己的一套查詢語言。既然是數(shù)據(jù)庫,有一些概念是互通的,如下表:
三、菜鳥實戰(zhàn)
基礎(chǔ)環(huán)境準(zhǔn)備可參考之前文章。
創(chuàng)建索引
發(fā)送創(chuàng)建請求
創(chuàng)建索引返回結(jié)果
PHP
// 創(chuàng)建索引
public function create(Request $request){
// 獲取索引名稱
$testIndex = $request->get("index_name", "test");
// 執(zhí)行
$client = EsHelper::getEsClient();
$params = [
'index' => $testIndex,
];
$response = $client->indices()->create($params);
// 返回
$data = [
'es_info' => $response->asArray(),
];
$this->success($data);
}
JSON
{
"code": 0,
"message": "ok",
"data": {
"es_info": {
"acknowledged": true,
"shards_acknowledged": true,
"index": "test"
}
}
}
更加復(fù)雜的參數(shù)
可指定主分片個數(shù):number_of_shards
可指定副分片個數(shù):number_of_replicas
PHP
$params = [
'index' => 'my_index',
'body' => [
'settings' => [
'number_of_shards' => 3,
'number_of_replicas' => 2
],
'mappings' => [
'my_type' => [
'_source' => [
'enabled' => true
],
'properties' => [
'first_name' => [
'type' => 'string',
'analyzer' => 'standard'
],
'age' => [
'type' => 'integer'
]
]
]
]
]
];
查詢索引詳情
發(fā)送查詢請求
PHP
// 查詢索引
public function detail(Request $request){
// 獲取索引名稱
$queryIndexName = $request->get("index_name", "test");
$params = [
'index' => $queryIndexName,
];
// 執(zhí)行
$client = EsHelper::getEsClient();
$result = "";
try {
$response = $client->indices()->get($params);
$result = $response->asArray();
}catch (\Exception $e){
$result = $e->getMessage();
}
// 返回
$data = [
'es_info' => $result,
];
$this->success($data);
}
響應(yīng)結(jié)果
JSON
{
"code": 0,
"message": "ok",
"data": {
"es_info": {
"test1": {
"aliases": [],
"mappings": [],
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "test1",
"creation_date": "1669179904968",
"number_of_replicas": "1",
"uuid": "Y62XH2Z4RC-kd2cKieOu9Q",
"version": {
"created": "8050199"
}
}
}
}
}
}
}
更新索引
發(fā)送更新索引請求
PHP
// 更新索引
public function update(Request $request){
// 獲取索引名稱
$indexName = $request->get("index_name", "test");
$number_of_replicas = $request->get("number_of_replicas", 1);
// 確定參數(shù)
$params = [
'index' => $indexName,
'body' => [
'settings' => [
'number_of_replicas' => $number_of_replicas,
]
]
];
// 執(zhí)行
$client = EsHelper::getEsClient();
try {
$response = $client->indices()->putSettings($params);
$result = $response->asArray();
}catch (\Exception $e){
$result = $e->getMessage();
}
// 返回
$data = [
'es_info' => $result,
];
$this->success($data);
}
響應(yīng)結(jié)果
JSON
{
"code": 0,
"message": "ok",
"data": {
"es_info": {
"acknowledged": true
}
}
}
刪除索引
發(fā)送刪除索引請求
PHP
// 刪除索引
public function delete(Request $request){
// 獲取索引名稱
$indexName = $request->get("index_name", "test1");
// 確定參數(shù)
$params = [
'index' => $indexName,
];
// 執(zhí)行
$client = EsHelper::getEsClient();
try {
$response = $client->indices()->delete($params);
$result = $response->asArray();
}catch (\Exception $e){
$result = $e->getMessage();
}
// 返回
$data = [
'es_info' => $result,
];
$this->success($data);
}
響應(yīng)結(jié)果 文章來源:http://www.zghlxwxcb.cn/news/detail-535668.html
JSON
{
"code": 0,
"message": "ok",
"data": {
"es_info": {
"acknowledged": true
}
}
}
通過上述步驟,就將 php 與 Elasticsearch 的索引操作連通了,接下來就可以在索引上創(chuàng)建文檔了。文章來源地址http://www.zghlxwxcb.cn/news/detail-535668.html
到了這里,關(guān)于PHP 如何使用 Elasticsearch 的 索引 API 接口的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!