|
@@ -0,0 +1,197 @@
|
|
|
|
|
+package com.sckw.elasticsearch.service.es;
|
|
|
|
|
+
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
|
|
|
|
|
+import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
|
|
|
|
+import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
|
|
|
|
|
+import org.elasticsearch.action.bulk.BulkRequest;
|
|
|
|
|
+import org.elasticsearch.action.bulk.BulkResponse;
|
|
|
|
|
+import org.elasticsearch.action.delete.DeleteRequest;
|
|
|
|
|
+import org.elasticsearch.action.search.ClearScrollRequest;
|
|
|
|
|
+import org.elasticsearch.action.search.SearchRequest;
|
|
|
|
|
+import org.elasticsearch.action.search.SearchResponse;
|
|
|
|
|
+import org.elasticsearch.action.support.IndicesOptions;
|
|
|
|
|
+import org.elasticsearch.client.RequestOptions;
|
|
|
|
|
+import org.elasticsearch.client.RestHighLevelClient;
|
|
|
|
|
+import org.elasticsearch.client.core.CountRequest;
|
|
|
|
|
+import org.elasticsearch.client.core.CountResponse;
|
|
|
|
|
+import org.elasticsearch.common.settings.Settings;
|
|
|
|
|
+import org.elasticsearch.common.xcontent.XContentType;
|
|
|
|
|
+import org.elasticsearch.search.SearchHits;
|
|
|
|
|
+import org.elasticsearch.search.aggregations.Aggregations;
|
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author yuzhongchuan
|
|
|
|
|
+ * @description COMMENT='ES相关工具类'
|
|
|
|
|
+ * @create 2023 -06 -06 10:01
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class EsUtils {
|
|
|
|
|
+
|
|
|
|
|
+ private RestHighLevelClient client;
|
|
|
|
|
+
|
|
|
|
|
+ private static EsUtils esUtils;
|
|
|
|
|
+
|
|
|
|
|
+ public EsUtils(RestHighLevelClient client) {
|
|
|
|
|
+ esUtils = this;
|
|
|
|
|
+ this.client = client;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 创建索引
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param idxName
|
|
|
|
|
+ * @param idxSql
|
|
|
|
|
+ * @throws Exception
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void createIndex(String idxName,String idxSql){
|
|
|
|
|
+ if (!indexExist(idxName)) {
|
|
|
|
|
+ log.error(" idxName={} 已经存在,idxSql={}",idxName,idxSql);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info(" idxName={} 创建索引,idxSql={}",idxName,idxSql);
|
|
|
|
|
+ CreateIndexRequest request = new CreateIndexRequest(idxName);
|
|
|
|
|
+ buildSetting(request);
|
|
|
|
|
+ request.mapping("_doc",idxSql, XContentType.JSON);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 删除索引
|
|
|
|
|
+ * @param idxName 索引名称
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void deleteIndex(String idxName){
|
|
|
|
|
+ if (!indexExist(idxName)) {
|
|
|
|
|
+ log.error(" idxName={} 索引不存在",idxName);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ esUtils.client.indices().delete(new DeleteIndexRequest(idxName),RequestOptions.DEFAULT);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ log.error("ES deleteIndex异常", e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 设置分片
|
|
|
|
|
+ * @param request
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void buildSetting(CreateIndexRequest request){
|
|
|
|
|
+ request.settings(Settings.builder().put("index.number_of_shards",9)
|
|
|
|
|
+ .put("index.number_of_replicas",1));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 断某个index是否存在
|
|
|
|
|
+ * @param idxName
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static boolean indexExist(String idxName) {
|
|
|
|
|
+ GetIndexRequest request = new GetIndexRequest();
|
|
|
|
|
+ request.local(false);
|
|
|
|
|
+ request.humanReadable(true);
|
|
|
|
|
+ request.includeDefaults(false);
|
|
|
|
|
+ request.indices(idxName);
|
|
|
|
|
+ request.indicesOptions(IndicesOptions.lenientExpandOpen());
|
|
|
|
|
+ try {
|
|
|
|
|
+ return esUtils.client.indices().exists(request,RequestOptions.DEFAULT);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ log.error("ES indexExist查询SearchRequest异常", e);
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 布尔查询
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param searchRequest
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static SearchHits boolSearch(SearchRequest searchRequest) {
|
|
|
|
|
+ log.info("ES开始boolSearch查询SearchRequest:{}", searchRequest);
|
|
|
|
|
+ try {
|
|
|
|
|
+ SearchResponse searchResponse = esUtils.client.search(searchRequest, RequestOptions.DEFAULT);
|
|
|
|
|
+ return searchResponse.getHits();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("ES boolSearch查询SearchRequest异常", e);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 分组聚合查询
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param searchRequest
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Aggregations groupBySearch(SearchRequest searchRequest) {
|
|
|
|
|
+ log.info("ES开始groupBySearch查询SearchRequest:{}", searchRequest);
|
|
|
|
|
+ try {
|
|
|
|
|
+ SearchResponse searchResponse = esUtils.client.search(searchRequest, RequestOptions.DEFAULT);
|
|
|
|
|
+ return searchResponse.getAggregations();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("ES groupBySearch查询SearchRequest异常", e);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 清除滚屏
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param scrollId
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void clearScroll(String scrollId) {
|
|
|
|
|
+ if (scrollId != null) {
|
|
|
|
|
+ ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
|
|
|
|
|
+ clearScrollRequest.addScrollId(scrollId);
|
|
|
|
|
+ try {
|
|
|
|
|
+ esUtils.client.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
|
|
|
|
|
+ log.info("ES清除滚屏成功");
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("ES清除滚屏出错:" + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 简单聚合查询
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param countRequest
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static long count(CountRequest countRequest ){
|
|
|
|
|
+ try {
|
|
|
|
|
+
|
|
|
|
|
+ log.info("开始ES查询indexName:{}的count", Arrays.toString(countRequest.indices()));
|
|
|
|
|
+ CountResponse count = esUtils.client.count(countRequest, RequestOptions.DEFAULT);
|
|
|
|
|
+ log.info("ES查询到:count={}", count.getCount());
|
|
|
|
|
+ return count.getCount();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("ES count异常", e);
|
|
|
|
|
+ return 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 批量新增
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param bulkRequest
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static boolean bulkIndex(BulkRequest bulkRequest) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ esUtils.client.bulk(bulkRequest, RequestOptions.DEFAULT);
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("批量插入数据异常:", e);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|