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->delete($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; } }