iivannov / elastic-commander
官方Elasticsearch PHP客户端的包装器
6.7.2
2022-03-09 15:47 UTC
Requires
- php: >=7.1
- elasticsearch/elasticsearch: ^6.7
README
这是一个简单易用的官方底层Elasticsearch PHP客户端的包装器。
它通过提供大量辅助方法来简化与Elasticsearch API的工作,包括索引管理、添加和更新文档以及搜索索引。
安装
通过Composer
$composer require iivannov/elastic-commander
使用方法
初始化
最低要求(将与默认主机 - localhost:9200)一起工作
$commander = new Commander('YourIndexName');
使用主机列表
$hosts = [
'192.168.1.1:9200', // IP + Port
'192.168.1.2', // Just IP
'mydomain.server.com:9201', // Domain + Port
'mydomain2.server.com', // Just Domain
'https://', // SSL to localhost
'https://192.168.1.3:9200' // SSL to IP + Port
];
$commander = new Commander('YourIndexName', $hosts);
使用自定义处理程序
$handler = new MyCustomHandler();
$commander = new Commander('YourIndexName', $hosts, $handler);
注意。Commander的每个实例在初始化时都使用指定的索引名称。所有操作和查询都将在此索引上执行。如果您想设置新的索引名称以进行工作,请使用
$commander->reset('NewIndexName');
索引
1. 创建索引
$commander->index()->create();
2. 删除索引
$commander->index()->delete();
3. 重置索引
一个辅助方法,通过删除并重新创建索引来重置索引。
$commander->index()->reset();
4. 优化索引
// 未完成
$commander->index()->optimize();
5. 索引统计信息
// 未完成
$commander->index()->stats();
映射
1. 设置映射
$commander->mapping($mapping);
文档
1. 通过id检查文档是否存在
$commander->document('SomeDocumentType')->exists($id);
2. 通过id获取文档
$commander->document('SomeDocumentType')->get($id);
3. 添加文档
在当前版本中,添加方法期望文档有一个ID。
$commander->document('SomeDocumentType')->add($id, $parameters);
4. 更新文档
$commander->document('SomeDocumentType')->add($id, $parameters);
搜索
搜索是通过传递原始查询数组或使用自定义标准类来完成的。在查询之后,您可以选择获取:完整的响应、仅ID、结果映射或总数。
1. 原始查询
$result = $commander->search('SomeDocumentType')->query($query, $sort, $size, $from);
2. 标准类查询
$result = $commander->search('SomeDocumentType')->criteria($criteria);
结果
在运行原始查询或带有标准类查询后,您可以使用辅助方法之一来操作结果响应。
1. 返回ElasticSearch原始响应
$result->response();
2. 返回结果总数
$result->total();
3. 返回找到的命中项的ID数组
$result->ids();
3. 返回找到的命中项的对象数组
键将是命中项的_id,值将是一个stdClass对象,包含_source
$result->hits();
计数
// 未完成