$index_name, //索引名称 'body' => $body ]; // 创建索引 try { $response = $client->indices()->create($params); // 记录日志 $uid = defined('UID') ? UID : ''; platformLog($params, $response, 'alibaba_cloud_elastic_search_create_index_uid_' . $uid); return $response; } catch (\Exception $e) { $message = $e->getMessage(); $message = json_decode($message, true); // 记录日志 $uid = defined('UID') ? UID : ''; platformLog($params, $message, 'alibaba_cloud_elastic_search_create_index_uid_' . $uid); return $message['error']; } } /** * 获取索引信息 * @param string $index_name 索引名称 * @date 2021-04-15 */ public function getIndex($index_name) { $client = self::buildClient(); $params = [ 'index' => $index_name, //索引名称 ]; // 创建索引 try { $response = $client->indices()->getMapping($params); // 记录日志 $uid = defined('UID') ? UID : ''; platformLog($params, $response, 'alibaba_cloud_elastic_search_get_index_uid_' . $uid); return $response; } catch (\Exception $e) { $message = $e->getMessage(); $message = json_decode($message, true); // 记录日志 $uid = defined('UID') ? UID : ''; platformLog($params, $message, 'alibaba_cloud_elastic_search_get_index_uid_' . $uid); return $message['error']['root_cause']['0']; } } /** * 更新索引信息 * @param string $index_name 索引名称 * @param array $body * @date 2021-04-15 */ public function updateIndex($index_name, $body) { $client = self::buildClient(); $params = [ 'index' => $index_name, //索引名称 'body' => $body ]; // 创建索引 try { $response = $client->indices()->putSettings($params); // 记录日志 $uid = defined('UID') ? UID : ''; platformLog($params, $response, 'alibaba_cloud_elastic_search_update_index_uid_' . $uid); return $response; } catch (\Exception $e) { $message = $e->getMessage(); $message = json_decode($message, true); // 记录日志 $uid = defined('UID') ? UID : ''; platformLog($params, $message, 'alibaba_cloud_elastic_search_update_index_uid_' . $uid); return $message['error']['root_cause']['0']; } } /** * 新建文档 * @param string $index_name 索引名称 * @param array $body * @param string $document_id 文档ID,非必填,不填写时自动生成 * @date 2021-04-12 */ public function createDocment($index_name, $body, $document_id = '') { $client = self::buildClient(); $indexParams = [ 'index' => $index_name, 'body' => $body, // 'client' => [ // 'timeout' => 10, // 'connect_timeout' => 10 // ] ]; if (!empty($document_id)) { $indexParams['id'] = $document_id; //文档ID } // 插入数据 try { $indexResponse = $client->index($indexParams); // 记录日志 $uid = defined('UID') ? UID : ''; platformLog($indexParams, $indexResponse, 'alibaba_cloud_elastic_search_create_document_uid_' . $uid); return $indexResponse; } catch (\Exception $e) { $message = $e->getMessage(); $message = json_decode($message, true); // 记录日志 $uid = defined('UID') ? UID : ''; platformLog($indexParams, $message, 'alibaba_cloud_elastic_search_create_document_uid_' . $uid); return $message; } } /** * 更新文档 * @param string $index_name 索引名称 * @param array $body * @param string $document_id 文档ID * @date 2021-04-12 */ public function updateDocment($index_name, $body, $document_id) { $client = self::buildClient(); $params = [ 'index' => $index_name, 'body' => $body, ]; if (!empty($document_id)) { $params['id'] = $document_id; //文档ID } // 插入数据 try { $response = $client->update($params); // 记录日志 $uid = defined('UID') ? UID : ''; platformLog($params, $response, 'alibaba_cloud_elastic_search_update_document_uid_' . $uid); return $response; } catch (\Exception $e) { $message = $e->getMessage(); $message = json_decode($message, true); // 记录日志 $uid = defined('UID') ? UID : ''; platformLog($params, $message, 'alibaba_cloud_elastic_search_update_document_uid_' . $uid); return $message; } } /** * 删除文档 * @param string $index_name 索引名称 * @param string $document_id 文档ID,非必填,不填写时自动生成 * @date 2021-04-13 */ public function delDocment($index_name, $document_id = '') { $client = self::buildClient(); $params = [ 'index' => $index_name, 'id' => $document_id ]; // 插入数据 try { $indexResponse = $client->delete($params); // 记录日志 $uid = defined('UID') ? UID : ''; platformLog($params, $indexResponse, 'alibaba_cloud_elastic_search_del_document_uid_' . $uid); return $indexResponse; } catch (\Exception $e) { $message = $e->getMessage(); $message = json_decode($message, true); // 记录日志 $uid = defined('UID') ? UID : ''; platformLog($params, $message, 'alibaba_cloud_elastic_search_del_document_uid_' . $uid); return $message; } } /** * 搜索数据 * @param string $index_name 索引名称 * @param array $body * @param boolean $start_page 是否分页 false--不分页 true--分页 * @param integer $from 开始位置 * @param integer $per_page_number 每页显示数量 * @date 2021-03-26 */ public function search($index_name, $body, $start_page = false, $from = 0, $per_page_number = 50) { $client = self::buildClient(); $params = [ 'index' => $index_name, 'body' => $body, 'client' => [ 'timeout' => 10, 'connect_timeout' => 10 ] ]; if ($start_page) { $params['from'] = $from; $params['size'] = $per_page_number; } // 查询数据 try { $response = $client->search($params); // 记录日志 $uid = defined('UID') ? UID : ''; platformLog($params, $response, 'alibaba_cloud_elastic_search_search_uid_' . $uid); return $response; } catch (\Exception $e) { $message = $e->getMessage(); $message = json_decode($message, true); // 记录日志 $uid = defined('UID') ? UID : ''; platformLog($params, $message, 'alibaba_cloud_elastic_search_search_uid_' . $uid); return $message['error']; } } }