+ */ + private String id; + + /** + * 文档内容 + */ + private T data; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Object getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } + + public ElasticSearchDocModel() { + } + + public ElasticSearchDocModel(String id, T data) { + this.id = id; + this.data = data; + } +} diff --git a/adc-da-report/src/main/java/com/adc/da/report/client/ElasticSearchRestApiClient.java b/adc-da-report/src/main/java/com/adc/da/report/client/ElasticSearchRestApiClient.java new file mode 100644 index 0000000..3c80299 --- /dev/null +++ b/adc-da-report/src/main/java/com/adc/da/report/client/ElasticSearchRestApiClient.java @@ -0,0 +1,500 @@ +package com.adc.da.report.client; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.adc.da.report.exception.ElasticSearchRunException; +import com.adc.da.report.util.PageUtils; +import lombok.extern.slf4j.Slf4j; +import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.ElasticsearchStatusException; +import org.elasticsearch.action.DocWriteResponse; +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest; +import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse; +import org.elasticsearch.action.bulk.BulkRequest; +import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.delete.DeleteResponse; +import org.elasticsearch.action.get.*; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.action.update.UpdateRequest; +import org.elasticsearch.action.update.UpdateResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; +import org.elasticsearch.client.indices.GetIndexRequest; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.reindex.DeleteByQueryRequest; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.search.fetch.subphase.FetchSourceContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * ElasticSearch 客户端 RestHighLevelClient Api接口封装 + *
+ * 官方Api地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.x/java-rest-high.html
+ *
+ */
+@Slf4j
+@Component
+public class ElasticSearchRestApiClient {
+
+ @Autowired
+ private RestHighLevelClient restHighLevelClient;
+
+ /**
+ * 默认主分片数
+ */
+ private static final int DEFAULT_SHARDS = 3;
+ /**
+ * 默认副本分片数
+ */
+ private static final int DEFAULT_REPLICAS = 2;
+
+ /**
+ * 判断索引是否存在
+ *
+ * @param index 索引
+ * @return 返回 true,表示存在
+ */
+ public boolean existsIndex(String index) {
+ try {
+ GetIndexRequest request = new GetIndexRequest(index);
+ request.local(false);
+ request.humanReadable(true);
+ request.includeDefaults(false);
+
+ return restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
+ } catch (IOException e) {
+ log.error("[ elasticsearch ] >> get index exists exception ,index:{} ", index, e);
+ throw new ElasticSearchRunException("[ elasticsearch ] >> get index exists exception {}", e);
+ }
+ }
+
+ /**
+ * 创建 ES 索引
+ *
+ * @param index 索引
+ * @param properties 文档属性集合
+ * @return 返回 true,表示创建成功
+ */
+ public boolean createIndex(String index, Map
+ * 如果文档存在,则更新文档;如果文档不存在,则保存文档。
+ *
+ * @param index 索引
+ * @param id 数据ID
+ * @param dataValue 数据内容
+ */
+ public IndexResponse saveOrUpdate(String index, String id, Object dataValue) {
+ try {
+ IndexRequest request = new IndexRequest(index);
+ request.id(id);
+ request.source(JSON.toJSONString(dataValue), XContentType.JSON);
+ return restHighLevelClient.index(request, RequestOptions.DEFAULT);
+ } catch (IOException e) {
+ log.error("[ elasticsearch ] >> save exception ,index = {},dataValue={} ,stack={}", index, dataValue, e);
+ throw new ElasticSearchRunException("[ elasticsearch ] >> save exception {}", e);
+ }
+ }
+
+ /**
+ * 批量-新增或保存文档
+ *
+ * 如果集合中有些文档已经存在,则更新文档;不存在,则保存文档。
+ *
+ * @param index 索引
+ * @param documentList 文档集合
+ */
+ public void batchSaveOrUpdate(String index, List