PHP 使用 Elasticsearch 的 索引 API 接口
目录
一、实战场景
如何在 PHP 中使用 Elasticsearch 的索引 API 接口
二、知识点
Elasticsearch 本质上是一个数据库,但并不是 MySQL 这种关系型数据库,查询语言也不是 SQL,而是 Elasticsearch 自己的一套查询语言。既然是数据库,有一些概念是互通的,如下表:
三、菜鸟实战
基础环境准备可参考之前文章。
创建索引
发送创建请求
创建索引返回结果
-
PHP
-
-
// 创建索引
-
-
public function create(Request $request){
-
-
// 获取索引名称
-
-
$testIndex = $request->get("index_name", "test");
-
-
-
// 执行
-
-
$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"
-
-
}
-
-
}
-
-
}
更加复杂的参数
可指定主分片个数:number_of_shards
可指定副分片个数: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'
-
-
]
-
-
]
-
-
]
-
-
]
-
-
]
-
-
];
查询索引详情
发送查询请求
-
PHP
-
-
// 查询索引
-
-
public function detail(Request $request){
-
-
// 获取索引名称
-
-
$queryIndexName = $request->get("index_name", "test");
-
-
$params = [
-
-
'index' => $queryIndexName,
-
-
];
-
-
// 执行
-
-
$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);
-
-
}
响应结果
-
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"
-
-
}
-
-
}
-
-
}
-
-
}
-
-
}
-
-
}
-
-
}
更新索引
发送更新索引请求
-
PHP
-
-
// 更新索引
-
-
public function update(Request $request){
-
-
// 获取索引名称
-
-
$indexName = $request->get("index_name", "test");
-
-
$number_of_replicas = $request->get("number_of_replicas", 1);
-
-
-
// 确定参数
-
-
$params = [
-
-
'index' => $indexName,
-
-
'body' => [
-
-
'settings' => [
-
-
'number_of_replicas' => $number_of_replicas,
-
-
]
-
-
]
-
-
];
-
-
-
// 执行
-
-
$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);
-
-
}
响应结果
-
JSON
-
-
{
-
-
"code": 0,
-
-
"message": "ok",
-
-
"data": {
-
-
"es_info": {
-
-
"acknowledged": true
-
-
}
-
-
}
-
-
}
删除索引
发送删除索引请求
-
PHP
-
-
// 删除索引
-
-
public function delete(Request $request){
-
-
// 获取索引名称
-
-
$indexName = $request->get("index_name", "test1");
-
-
-
// 确定参数
-
-
$params = [
-
-
'index' => $indexName,
-
-
];
-
-
-
// 执行
-
-
$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);
-
-
}
响应结果
-
JSON
-
-
{
-
-
"code": 0,
-
-
"message": "ok",
-
-
"data": {
-
-
"es_info": {
-
-
"acknowledged": true
-
-
}
-
-
}
-
-
}
通过上述步骤,就将 php 与 Elasticsearch 的索引操作连通了,接下来就可以在索引上创建文档了。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhhbajba
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
excel下划线不显示怎么办
PHP中文网 06-23 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel图片置于文字下方的方法
PHP中文网 06-27 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22