安装
再es安装篇章,我们介绍了es依赖的安装。
我们来给他做个封装。
参考文档:https://www.cnblogs.com/codeAB/p/10283304.html
他的文章使用6.5。他封装的东西也有点老旧了。
所以我们稍微改了下,直接上代码。
<?php
namespace app\ser;
class EsServer {
private $EsClient = null;
public $index = '';
private $type = '';
public function __construct($index = '', $host = ['127.0.0.1']) {
/**
* 实例化 ES 客户端
*/
$this->EsClient = \Elasticsearch\ClientBuilder::create()->setHosts($host)->build();
if (empty($index)) {
return ['code' => 1001, 'msg' => 'index不能为空'];
}
$this->index = $index;
}
/**
* 增
* 添加一个文档到 Index 的Type中
* @param array $body
* @return void
*/
public function putDoc($body = [], $id = '') {
if (!$this->checkIndexExists()) {
return ['code' => 1001, 'msg' => '未创建index'];
}
$params = [
'index' => $this->index,
'body' => $body
];
if (!empty($id)) {
$params['id'] = $id;
}
return $this->EsClient->index($params);
}
/**
* 批量新增文档
* @param type $res
* @return type
*/
public function putDocs($res = []) {
foreach ($res as $key => $val) {
unset($index);
$index['index']['_index'] = $this->index;
if (!empty($val['id'])) {
$index['index']['_id'] = $val['id'];
}
$params['body'][] = $index;
unset($val['id']);
$params['body'][] = $val;
}
$responses = $this->EsClient->bulk($params);
return $responses;
}
/**
* 删
* 删除一个文档
* @param $id
* @return array
*/
public function delDoc($id, $param = []) {
if (empty($id)) {
$params = [
'index' => $this->index,
'type' => $this->type,
'id' => $id
];
} else {
$params = $param;
}
return $this->EsClient->delete($params);
}
/**
* 改
* 更新一个文档
* @param $id
* @return array
*/
public function updateDoc($id, $data) {
$params = [
'index' => $this->index,
'id' => $id,
'body' => [
'doc' => $data
]
];
return $this->EsClient->update($params);
}
/**
* 查
* 获取单个文档
* @param $id
* @return array
*/
public function getDoc($id) {
$params = [
'index' => $this->index,
'id' => $id
];
return $this->EsClient->get($params);
}
/**
* 一次获取多个文档
* @param $ids
* @return array
*/
public function getDocs($ids) {
$params = [
'index' => $this->index,
'body' => ['ids' => $ids],
];
return $this->EsClient->mget($params);
}
/**
* 搜
* 搜索文档,query是查询条件
* @param array $query
* @param int $from
* @param int $size
* @return array
*/
public function search($query = [], $field = '*', $page = 0, $limit = 10) {
$from = $page * $limit;
$params = [
'index' => $this->index,
'_source' => $field, // 请求指定的字段
'body' => array_merge([
'from' => $from,
'size' => $limit
], $query)
];
return $this->EsClient->search($params);
}
/**
* 删除所有的 Index
*/
public function delAllIndex() {
$indexList = $this->esStatus()['indices'];
// $a = array_keys($indexList);
// dump($a);
foreach ($indexList as $item => $index) {
$this->delIndex($item);
}
return true;
}
/**
* 删除一个Index
* @return void
*/
public function delIndex($index = '') {
$params = [
'index' => !empty($index) ? $index : $this->index
];
if ($this->checkIndexExists($index)) {
return $this->EsClient->indices()->delete($params);
}
}
/**
* 检查Index 是否存在
* @return bool
*/
public function checkIndexExists($index = '') {
$params = [
'index' => !empty($index) ? $index : $this->index
];
return $this->EsClient->indices()->exists($params);
}
/**
* 获取 ES 的状态信息,包括index 列表
* @return array
*/
public function esStatus() {
return $this->EsClient->indices()->stats();
}
/**
* 创建一个索引 Index (非关系型数据库里面那个索引,而是关系型数据里面的数据库的意思)
* @return void
*/
public function createIndex() {
$this->delIndex();
$params = [
'index' => $this->index,
];
return $this->EsClient->indices()->create($params);
}
/**
*
* @param type $param
* @return type
*
* 例子: 'first_name' => [字段名
// 'type' => 'text', 字段类型 具体搜文档
// 'analyzer' => 'ik_max_word' 分词策略ik_max_word:最细粒度拆分,ik_smart最粗粒度拆分 如果没分词需要可不填该字段
// ]
*/
public function createMapping($param) {
if (!$this->checkIndexExists()) {
$this->createIndex();
}
$params = [
'index' => $this->index,
'body' => [
'_source' => [
'enabled' => true
],
'properties' => [
$param
]
]
];
try {
return $this->EsClient->indices()->putMapping($params);
} catch (\Exception $ex) {
return $ex->getMessage();
}
}
}
我们自行封装几个常用的东西。
1.新增文档(putDoc)|批量新增文档(putDocs)
2.删除文档(delDoc)delDoc
3.更新文档(updateDoc)
4.查询文档(getDoc)|批量查询文档(getDocs)
5.搜索(search)
6.删除所有索引(delAllIndex)|删除单个索引(delIndex)
7.验证索引是否存在(checkIndexExists)
8.获取es状态(esStatus)
9.创建索引(createIndex)
10.创建映射(createMapping)
1-5就是curd操作,类似mysql的增删改查。
6-10索引操作,类似mysql中对创建表,删除表,定义字段属性,
ps:我们可以把索引的意思,当成mysql中的表名。
文档更新时间: 2020-09-07 15:25 作者:young