123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace App\Components;
- use Elasticsearch\ClientBuilder;
- class EsNative {
- private $client;
- public function __construct(array $config) {
- $host = $config['host'];
- $port = $config['port'];
- $user = $config['user'];
- $pass = $config['pass'];
- $this->client = ClientBuilder::create()->setHosts([
- [
- 'host' => $host,
- 'port' => $port,
- 'scheme' => 'http',
- 'user' => $user,
- 'pass' => $pass
- ]
- ])->setConnectionPool('\Elasticsearch\ConnectionPool\SimpleConnectionPool', [])
- ->setRetries(10)->build();
- }
- // -------------------------------- 索引操作
- /**
- * 创建索引
- * @param $params
- * @return array|string
- */
- public function createIdx($params) {
- try {
- $resp = $this->client->indices()->create($params);
- } catch (\Exception $e) {
- $resp = $e->getMessage();
- }
- return $resp;
- }
- /**
- * 删除索引
- * @param $index
- * @return array|callable|string
- */
- public function deleteIdx($index) {
- $data = [
- 'index' => $index
- ];
- try {
- $resp = $this->client->indices()->delete($data);
- } catch (\Exception $e) {
- $resp = $e->getMessage();
- }
- return $resp;
- }
- /**
- * 是否存在索引
- * @param $index
- * @return array|callable|string
- */
- public function existsIdx($index) {
- $data = [
- 'index' => $index
- ];
- try {
- $resp = $this->client->indices()->exists($data);
- } catch (\Exception $e) {
- $resp = $e->getMessage();
- }
- return $resp;
- }
- // -------------------------------- 文档操作
- /**
- * 插入数据
- * @param $params
- * @return array|callable|string
- */
- public function addDoc($params) {
- try {
- $resp = $this->client->index($params);
- } catch (\Exception $e) {
- $resp = $e->getMessage();
- }
- return $resp;
- }
- /**
- * 更新数据
- * @param $params
- * @return array|callable|string
- */
- public function updateDoc($params) {
- try {
- $resp = $this->client->update($params);
- } catch (\Exception $e) {
- $resp = $e->getMessage();
- }
- return $resp;
- }
- /**
- * 删除数据
- * @param $params
- * @return array|callable|string
- */
- public function deleteDoc($params) {
- try {
- $resp = $this->client->delete($params);
- } catch (\Exception $e) {
- $resp = $e->getMessage();
- }
- return $resp;
- }
- /**
- * 查询数据
- * @param $params
- * @return array|callable|string
- */
- public function searchDoc($params) {
- try {
- $resp = $this->client->search($params);
- } catch (\Exception $e) {
- $resp = $e->getMessage();
- }
- return $resp;
- }
- /**
- * 批量操作
- * @param $data
- * @return array|callable|string
- */
- public function bulkDoc($data)
- {
- try {
- $resp = $this->client->bulk($data);
- } catch (\Exception $e) {
- $resp = $e->getMessage();
- }
- return $resp;
- }
- /**
- * 计算多少条数
- * @param $params
- * @return array|callable|string
- */
- public function countDoc($params) {
- try {
- $resp = $this->client->count($params);
- } catch (\Exception $e) {
- $resp = $e->getMessage();
- }
- return $resp;
- }
- // -------------------------------- 其他
- /**
- * 添加别名
- * @param $params
- * @return array|string
- */
- public function addAlias($params)
- {
- try {
- $resp = $this->client->indices()->updateAliases($params);
- } catch (\Exception $e) {
- $resp = $e->getMessage();
- }
- return $resp;
- }
- }
|