ElasticSerach.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace Tool\ShanTaoTool\ElasticSearch;
  3. use function Complex\sec;
  4. use Elasticsearch\ClientBuilder;
  5. use Tool\ShanTaoTool\Bean\ElasticSearch\GetLogDocParamBean;
  6. use Tool\ShanTaoTool\Exception\ElasticSearchException;
  7. /**
  8. * es操作类
  9. * Class ElasticSerach
  10. * @package Tool\ShanTaoTool\ElasticSearch
  11. */
  12. class ElasticSerach
  13. {
  14. /**
  15. * @var $client \Elasticsearch\Client
  16. */
  17. static $client;
  18. /**
  19. * 获取es客户端
  20. */
  21. public static function getElasticSearchClient()
  22. {
  23. $host = env("ES_HOST");
  24. $port = env("ES_PORT");
  25. $user = env("ES_USER");
  26. $password = env("ES_PASSWORD");
  27. $hosts = ["{$host}:{$port}"];
  28. $clientBuilder = ClientBuilder::create();
  29. //设置es地址
  30. $clientBuilder->setHosts($hosts);
  31. //设置账号密码
  32. $clientBuilder->setBasicAuthentication($user,$password);
  33. //设置尝试次数
  34. $clientBuilder->setRetries(2);
  35. $client = $clientBuilder->build();
  36. self::$client = $client;
  37. }
  38. /**
  39. * 创建索引
  40. * @param $index 索引名称
  41. * @param $mapping 索引结构
  42. */
  43. public static function createIndex($index,$mapping=[])
  44. {
  45. if(!$index){
  46. throw new ElasticSearchException("索引不能为空");
  47. }
  48. $param = [
  49. "index"=>$index
  50. ];
  51. if($mapping){
  52. $param["body"]["mappings"] = $mapping;
  53. }
  54. return self::$client->indices()->create($param);
  55. }
  56. /**
  57. * 索引中写入文档
  58. * @param $index 索引名称
  59. * @param $data 文档数据
  60. * @param $id 文档ID
  61. */
  62. public static function createDoc($index, array $data,$id=0)
  63. {
  64. if(!$index){
  65. throw new ElasticSearchException("索引不能为空");
  66. }
  67. if(!$data){
  68. throw new ElasticSearchException("文档不能为空");
  69. }
  70. $params = [
  71. "index"=>$index,
  72. "body"=>$data
  73. ];
  74. if($id){
  75. $params["id"] = $id;
  76. }
  77. return self::$client->index($params);
  78. }
  79. /**
  80. * 批量写入文档
  81. * @param $index 索引名称
  82. * @param array $datas 文档数据
  83. */
  84. public static function batchCreateDoc($index, array $datas)
  85. {
  86. if(!$index){
  87. throw new ElasticSearchException("索引不能为空");
  88. }
  89. if(!$datas){
  90. throw new ElasticSearchException("文档不能为空");
  91. }
  92. if(count($datas)>100){
  93. throw new ElasticSearchException("文档数量不能大于100");
  94. }
  95. $params = [
  96. "index"=>$index,
  97. "body"=>[]
  98. ];
  99. foreach ($datas as $data){
  100. $params['body'][] = [
  101. 'index' => [
  102. '_index' => $index,
  103. ]
  104. ];
  105. $params['body'][] = $data;
  106. }
  107. return self::$client->bulk($params);
  108. }
  109. /**
  110. * 根据索引和文档ID获取文档
  111. * @param $index 索引名称
  112. * @param $id 文档ID
  113. */
  114. public static function getDoc($index, $id)
  115. {
  116. if(!$index){
  117. throw new ElasticSearchException("索引不能为空");
  118. }
  119. if(!$id){
  120. throw new ElasticSearchException("文档id不能为空");
  121. }
  122. $params = [
  123. "index"=>$index,
  124. "id"=>$id
  125. ];
  126. return self::$client->get($params);
  127. }
  128. /**
  129. * 更新文档信息
  130. * @param $index 索引名称
  131. * @param $id 文档ID
  132. * @param array $data 更新内容
  133. */
  134. public static function updateDoc($index, $id, array $data)
  135. {
  136. if(!$index){
  137. throw new ElasticSearchException("索引不能为空");
  138. }
  139. if(!$id){
  140. throw new ElasticSearchException("文档id不能为空");
  141. }
  142. if(!$data){
  143. throw new ElasticSearchException("更新内容不能为空");
  144. }
  145. $params = [
  146. "index"=>$index,
  147. "id"=>$id,
  148. "body"=>[
  149. "doc"=>$data
  150. ]
  151. ];
  152. return self::$client->update($params);
  153. }
  154. /**
  155. * 删除文档
  156. * @param $index 索引名称
  157. * @param $id 文档ID
  158. */
  159. public static function deleteDoc($index, $id)
  160. {
  161. if(!$index){
  162. throw new ElasticSearchException("索引不能为空");
  163. }
  164. if(!$id){
  165. throw new ElasticSearchException("文档id不能为空");
  166. }
  167. $params = [
  168. "index"=>$index,
  169. "id"=>$id
  170. ];
  171. return self::$client->delete($params);
  172. }
  173. /**
  174. * 根据索引和项目名称获取数据
  175. * @param GetLogDocParamBean $getLogDocParamBean
  176. * @return array
  177. * @throws ElasticSearchException
  178. */
  179. public static function getLogDoc(GetLogDocParamBean $getLogDocParamBean)
  180. {
  181. if(!$getLogDocParamBean->getIndex()){
  182. throw new ElasticSearchException("索引不能为空");
  183. }
  184. if(!$getLogDocParamBean->getProjectName()){
  185. throw new ElasticSearchException("项目名称不能为空");
  186. }
  187. $param = [
  188. "index"=>$getLogDocParamBean->getIndex(),
  189. "body"=>[
  190. "query"=>[
  191. "bool"=>[
  192. "must"=>[
  193. [
  194. "match"=>[
  195. "request_project_name"=>$getLogDocParamBean->getProjectName()
  196. ]
  197. ]
  198. ]
  199. ]
  200. ],
  201. "sort"=>[
  202. [
  203. "request_id"=>[
  204. "order"=>"desc"
  205. ]
  206. ]
  207. ]
  208. ]
  209. ];
  210. //判断是否存在路径
  211. if($getLogDocParamBean->getRequestPath()){
  212. $param["body"]["query"]["bool"]["must"][] = [
  213. "match"=>[
  214. "request_path_md5"=>md5($getLogDocParamBean->getRequestPath())
  215. ]
  216. ];
  217. }
  218. //判断是否存在参数
  219. if($getLogDocParamBean->getParam()){
  220. $param["body"]["query"]["bool"]["must"][] = [
  221. "match"=>[
  222. "request_param"=>$getLogDocParamBean->getParam()
  223. ]
  224. ];
  225. }
  226. //判断是否存在链路追踪ID
  227. if($getLogDocParamBean->getUniqueTraceId()){
  228. $param["body"]["query"]["bool"]["must"][] = [
  229. "match"=>[
  230. "unique_trace_id"=>$getLogDocParamBean->getUniqueTraceId()
  231. ]
  232. ];
  233. }
  234. //判断是否存在请求时间
  235. if ($getLogDocParamBean->getRequestStartTime()){
  236. $param["body"]["query"]["bool"]["must"][] = [
  237. "range"=>[
  238. "request_id"=>[
  239. "gte"=>strtotime($getLogDocParamBean->getRequestStartTime())*1000,
  240. "lte"=>strtotime($getLogDocParamBean->getRequestEndTime())*1000
  241. ]
  242. ]
  243. ];
  244. }
  245. //判断是否存在页数
  246. $page = $getLogDocParamBean->getPage();
  247. $pageSize = $getLogDocParamBean->getPageSize();
  248. if($page){
  249. $param["size"] = $pageSize;
  250. $param["from"] = ($page-1)*$pageSize;
  251. }
  252. return self::$client->search($param);
  253. }
  254. /**
  255. * 根据traceId获取链路数据
  256. * @param string $uniqueTraceId
  257. * @param string $index
  258. * @return array
  259. * @throws ElasticSearchException
  260. */
  261. public static function getTraceLogDoc(string $uniqueTraceId,$index)
  262. {
  263. $param = [
  264. "index"=>$index,
  265. "body"=>[
  266. "query"=>[
  267. "bool"=>[
  268. "must"=>[
  269. [
  270. "match"=>[
  271. "unique_trace_id"=>$uniqueTraceId
  272. ]
  273. ]
  274. ]
  275. ]
  276. ],
  277. "sort"=>[
  278. [
  279. "request_id"=>[
  280. "order"=>"asc"
  281. ]
  282. ]
  283. ]
  284. ]
  285. ];
  286. return self::$client->search($param);
  287. }
  288. /**
  289. * 删除索引
  290. * @param string $index
  291. */
  292. public static function deleteIndex($index)
  293. {
  294. if(!$index){
  295. throw new ElasticSearchException("索引不能为空");
  296. }
  297. $params = [
  298. "index"=>$index
  299. ];
  300. return self::$client->indices()->delete($params);
  301. }
  302. }