合并master到caihaohan
This commit is contained in:
@@ -118,6 +118,18 @@
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch</groupId>
|
||||
<artifactId>elasticsearch</artifactId>
|
||||
<version>7.6.2</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch.client</groupId>
|
||||
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||
<version>7.6.2</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.adc.da.report.client;
|
||||
|
||||
/**
|
||||
* ElasticSearch 批量操作公共model
|
||||
*
|
||||
* @author 程序员小强
|
||||
*/
|
||||
public class ElasticSearchDocModel<T> {
|
||||
|
||||
/**
|
||||
* 文档ID
|
||||
* <p>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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接口封装
|
||||
* <p>
|
||||
* 官方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<String, Object> properties) {
|
||||
try {
|
||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||
// 注:ES 7.x 后的版本中,已经弃用 type
|
||||
builder.startObject()
|
||||
.startObject("mappings")
|
||||
.field("properties", properties)
|
||||
.endObject()
|
||||
.startObject("settings")
|
||||
//分片数
|
||||
.field("number_of_shards", DEFAULT_SHARDS)
|
||||
//副本数
|
||||
.field("number_of_replicas", DEFAULT_REPLICAS)
|
||||
.endObject()
|
||||
.endObject();
|
||||
CreateIndexRequest request = new CreateIndexRequest(index).source(builder);
|
||||
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
|
||||
return response.isAcknowledged();
|
||||
} catch (IOException e) {
|
||||
log.error("[ elasticsearch ] >> createIndex exception ,index:{},properties:{}", index, properties, e);
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> createIndex exception ");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除索引
|
||||
*
|
||||
* @param index 索引
|
||||
* @return 返回 true,表示删除成功
|
||||
*/
|
||||
public boolean deleteIndex(String index) {
|
||||
try {
|
||||
DeleteIndexRequest request = new DeleteIndexRequest(index);
|
||||
AcknowledgedResponse response = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
|
||||
|
||||
return response.isAcknowledged();
|
||||
} catch (ElasticsearchException e) {
|
||||
//索引不存在-无需删除
|
||||
if (e.status() == RestStatus.NOT_FOUND) {
|
||||
log.error("[ elasticsearch ] >> deleteIndex >> index:{}, Not found ", index, e);
|
||||
return false;
|
||||
}
|
||||
log.error("[ elasticsearch ] >> deleteIndex exception ,index:{}", index, e);
|
||||
throw new ElasticSearchRunException("elasticsearch deleteIndex exception ");
|
||||
} catch (IOException e) {
|
||||
//其它未知异常
|
||||
log.error("[ elasticsearch ] >> deleteIndex exception ,index:{}", index, e);
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> deleteIndex exception {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取索引配置
|
||||
*
|
||||
* @param index 索引
|
||||
* @return 返回索引配置内容
|
||||
*/
|
||||
public GetSettingsResponse getIndexSetting(String index) {
|
||||
try {
|
||||
GetSettingsRequest request = new GetSettingsRequest().indices(index);
|
||||
return restHighLevelClient.indices().getSettings(request, RequestOptions.DEFAULT);
|
||||
} catch (IOException e) {
|
||||
//其它未知异常
|
||||
log.error("[ elasticsearch ] >> getIndexSetting exception ,index:{}", index, e);
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> getIndexSetting exception {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断文档是否存在
|
||||
*
|
||||
* @param index 索引
|
||||
* @return 返回 true,表示存在
|
||||
*/
|
||||
public boolean existsDocument(String index, String id) {
|
||||
try {
|
||||
GetRequest request = new GetRequest(index, id);
|
||||
//禁用获取_source
|
||||
request.fetchSourceContext(new FetchSourceContext(false));
|
||||
//禁用获取存储的字段。
|
||||
request.storedFields("_none_");
|
||||
|
||||
return restHighLevelClient.exists(request, RequestOptions.DEFAULT);
|
||||
} catch (IOException e) {
|
||||
log.error("[ elasticsearch ] >> get document exists exception ,index:{} ", index, e);
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> get document exists exception {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存数据-随机生成数据ID
|
||||
*
|
||||
* @param index 索引
|
||||
* @param dataValue 数据内容
|
||||
*/
|
||||
public IndexResponse save(String index, Object dataValue) {
|
||||
try {
|
||||
IndexRequest request = new IndexRequest(index);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存文档-自定义数据ID
|
||||
*
|
||||
* @param index 索引
|
||||
* @param id 数据ID
|
||||
* @param dataValue 数据内容
|
||||
*/
|
||||
public IndexResponse save(String index, String id, Object dataValue) {
|
||||
return this.saveOrUpdate(index, id, dataValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存文档-自定义数据ID
|
||||
* <p>
|
||||
* 如果文档存在,则更新文档;如果文档不存在,则保存文档。
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量-新增或保存文档
|
||||
* <p>
|
||||
* 如果集合中有些文档已经存在,则更新文档;不存在,则保存文档。
|
||||
*
|
||||
* @param index 索引
|
||||
* @param documentList 文档集合
|
||||
*/
|
||||
public void batchSaveOrUpdate(String index, List<ElasticSearchDocModel<?>> documentList) {
|
||||
if (CollectionUtils.isEmpty(documentList)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// 批量请求
|
||||
BulkRequest bulkRequest = new BulkRequest();
|
||||
documentList.forEach(doc -> bulkRequest.add(new IndexRequest(index)
|
||||
.id(doc.getId())
|
||||
.source(JSON.toJSONString(doc.getData()), XContentType.JSON)));
|
||||
restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
|
||||
} catch (IOException e) {
|
||||
log.error("[ elasticsearch ] >> batchSave exception ,index = {},documentList={} ,stack={}", index, documentList, e);
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> batchSave exception {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID修改
|
||||
*
|
||||
* @param index 索引
|
||||
* @param id 数据ID
|
||||
* @param dataValue 数据内容
|
||||
*/
|
||||
public UpdateResponse updateById(String index, String id, Object dataValue) {
|
||||
try {
|
||||
UpdateRequest request = new UpdateRequest(index, id);
|
||||
request.doc(JSON.toJSONString(dataValue), XContentType.JSON);
|
||||
return restHighLevelClient.update(request, RequestOptions.DEFAULT);
|
||||
} catch (IOException e) {
|
||||
log.error("[ elasticsearch ] >> updateById exception ,index = {},dataValue={} ,stack={}", index, dataValue, e);
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> updateById exception {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 部分修改()
|
||||
* 注:1).可变更已有字段值,可新增字段,删除字段无效
|
||||
* 2).若当前ID数据不存在则新增
|
||||
*
|
||||
* @param index 索引
|
||||
* @param id 数据ID
|
||||
* @param dataValue 数据内容
|
||||
*/
|
||||
public UpdateResponse updateByIdSelective(String index, String id, Object dataValue) {
|
||||
try {
|
||||
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(dataValue));
|
||||
UpdateRequest request = new UpdateRequest(index, id)
|
||||
.doc(jsonObject)
|
||||
.upsert(jsonObject);
|
||||
return restHighLevelClient.update(request, RequestOptions.DEFAULT);
|
||||
} catch (IOException e) {
|
||||
log.error("[ elasticsearch ] >> updateByIdSelective exception ,index = {},dataValue={} ,stack={}", index, dataValue, e);
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> updateByIdSelective exception {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id查询
|
||||
*
|
||||
* @param index 索引
|
||||
* @param id 数据ID
|
||||
* @return T
|
||||
*/
|
||||
public <T> T getById(String index, String id, Class<T> clazz) {
|
||||
GetResponse getResponse = this.getById(index, id);
|
||||
if (null == getResponse) {
|
||||
return null;
|
||||
}
|
||||
return JSON.parseObject(getResponse.getSourceAsString(), clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id集批量获取数据
|
||||
*
|
||||
* @param index 索引
|
||||
* @param idList 数据ID集
|
||||
* @return T
|
||||
*/
|
||||
public <T> List<T> getByIdList(String index, List<String> idList, Class<T> clazz) {
|
||||
MultiGetItemResponse[] responses = this.getByIdList(index, idList);
|
||||
if (null == responses || responses.length == 0) {
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
|
||||
List<T> resultList = new ArrayList<>(responses.length);
|
||||
for (MultiGetItemResponse response : responses) {
|
||||
GetResponse getResponse = response.getResponse();
|
||||
if (!getResponse.isExists()) {
|
||||
continue;
|
||||
}
|
||||
resultList.add(JSON.parseObject(getResponse.getSourceAsString(), clazz));
|
||||
}
|
||||
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据多条件查询--分页
|
||||
* 注:from-size -[ "浅"分页 ]
|
||||
*
|
||||
* @param index 索引
|
||||
* @param pageNo 页码(第几页)
|
||||
* @param pageSize 页容量- Elasticsearch默认配置单次最大限制10000
|
||||
*/
|
||||
public <T> List<T> searchPageByIndex(String index, Integer pageNo, Integer pageSize, Class<T> clazz) {
|
||||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
||||
searchSourceBuilder.from(PageUtils.getStartRow(pageNo, pageSize));
|
||||
searchSourceBuilder.size(pageSize);
|
||||
|
||||
return this.searchByQuery(index, searchSourceBuilder, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 条件查询
|
||||
*
|
||||
* @param index 索引
|
||||
* @param sourceBuilder 条件查询构建起
|
||||
* @param <T> 数据类型
|
||||
* @return T 类型的集合
|
||||
*/
|
||||
public <T> List<T> searchByQuery(String index, SearchSourceBuilder sourceBuilder, Class<T> clazz) {
|
||||
try {
|
||||
// 构建查询请求
|
||||
SearchRequest searchRequest = new SearchRequest(index).source(sourceBuilder);
|
||||
// 获取返回值
|
||||
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
|
||||
SearchHit[] hits = response.getHits().getHits();
|
||||
if (null == hits || hits.length == 0) {
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
|
||||
List<T> resultList = new ArrayList<>(hits.length);
|
||||
for (SearchHit hit : hits) {
|
||||
resultList.add(JSON.parseObject(hit.getSourceAsString(), clazz));
|
||||
}
|
||||
return resultList;
|
||||
} catch (ElasticsearchStatusException e) {
|
||||
//索引不存在
|
||||
if (e.status() == RestStatus.NOT_FOUND) {
|
||||
log.error("[ elasticsearch ] >> searchByQuery exception >> index:{}, Not found ", index, e);
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> searchByQuery exception {}", e);
|
||||
} catch (IOException e) {
|
||||
log.error("[ elasticsearch ] >> searchByQuery exception ,index = {},sourceBuilder={} ,stack={}", index, sourceBuilder, e);
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> searchByQuery exception {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据ID删除文档
|
||||
*
|
||||
* @param index 索引
|
||||
* @param id 文档ID
|
||||
* @return 是否删除成功
|
||||
*/
|
||||
public boolean deleteById(String index, String id) {
|
||||
try {
|
||||
DeleteRequest request = new DeleteRequest(index, id);
|
||||
DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
|
||||
//未找到文件
|
||||
if (response.getResult() == DocWriteResponse.Result.NOT_FOUND) {
|
||||
log.error("[ elasticsearch ] >> deleteById document is not found , index:{},id:{}", index, id);
|
||||
return false;
|
||||
}
|
||||
return RestStatus.OK.equals(response.status());
|
||||
} catch (IOException e) {
|
||||
log.error("[ elasticsearch ] >> deleteById exception ,index:{},id:{} ,stack:{}", index, id, e);
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> deleteById exception {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据查询条件删除文档
|
||||
*
|
||||
* @param index 索引
|
||||
* @param queryBuilder 查询条件构建器
|
||||
*/
|
||||
public void deleteByQuery(String index, QueryBuilder queryBuilder) {
|
||||
try {
|
||||
DeleteByQueryRequest request = new DeleteByQueryRequest(index).setQuery(queryBuilder);
|
||||
request.setConflicts("proceed");
|
||||
restHighLevelClient.deleteByQuery(request, RequestOptions.DEFAULT);
|
||||
} catch (IOException e) {
|
||||
log.error("[ elasticsearch ] >> deleteByQuery exception ,index = {},queryBuilder={} ,stack={}", index, queryBuilder, e);
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> deleteByQuery exception {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文档 ID 批量删除文档
|
||||
*
|
||||
* @param index 索引
|
||||
* @param idList 文档 ID 集合
|
||||
*/
|
||||
public void deleteByIdList(String index, List<String> idList) {
|
||||
if (CollectionUtils.isEmpty(idList)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
BulkRequest bulkRequest = new BulkRequest();
|
||||
idList.forEach(id -> bulkRequest.add(new DeleteRequest(index, id)));
|
||||
restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
|
||||
} catch (IOException e) {
|
||||
log.error("[ elasticsearch ] >> deleteByIdList exception ,index = {},idList={} ,stack={}", index, idList, e);
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> deleteByIdList exception {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id查询
|
||||
*
|
||||
* @param index 索引
|
||||
* @param id 文档ID
|
||||
* @return GetResponse
|
||||
*/
|
||||
private GetResponse getById(String index, String id) {
|
||||
try {
|
||||
GetRequest request = new GetRequest(index, id);
|
||||
return restHighLevelClient.get(request, RequestOptions.DEFAULT);
|
||||
} catch (ElasticsearchException e) {
|
||||
if (e.status() == RestStatus.NOT_FOUND) {
|
||||
log.error("[ elasticsearch ] >> getById document not found ,index = {},id={} ,stack={}", index, id, e);
|
||||
return null;
|
||||
}
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> getById exception {}", e);
|
||||
} catch (IOException e) {
|
||||
log.error("[ elasticsearch ] >> getById exception ,index = {},id={} ,stack={}", index, id, e);
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> getById exception {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id集-批量获取数据
|
||||
*
|
||||
* @param index 索引
|
||||
* @param idList 数据文档ID集
|
||||
* @return MultiGetItemResponse[]
|
||||
*/
|
||||
private MultiGetItemResponse[] getByIdList(String index, List<String> idList) {
|
||||
try {
|
||||
MultiGetRequest request = new MultiGetRequest();
|
||||
for (String id : idList) {
|
||||
request.add(new MultiGetRequest.Item(index, id));
|
||||
}
|
||||
|
||||
//同步执行
|
||||
MultiGetResponse responses = restHighLevelClient.mget(request, RequestOptions.DEFAULT);
|
||||
return responses.getResponses();
|
||||
} catch (IOException e) {
|
||||
log.error("[ elasticsearch ] >> getByIdList exception ,index = {},idList={} ,stack={}", index, idList, e);
|
||||
throw new ElasticSearchRunException("[ elasticsearch ] >> getByIdList exception {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.adc.da.report.config;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
|
||||
import org.elasticsearch.client.Node;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestClientBuilder;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* ElasticSearch Rest client 配置
|
||||
*
|
||||
* @author 程序员小强
|
||||
*/
|
||||
@Configuration
|
||||
public class ElasticSearchConfig {
|
||||
private static final Logger log = LoggerFactory.getLogger(ElasticSearchConfig.class);
|
||||
|
||||
@Resource
|
||||
private ElasticSearchProperty elasticSearchProperty;
|
||||
|
||||
@Bean
|
||||
public RestClientBuilder restClientBuilder() {
|
||||
Assert.notNull(elasticSearchProperty, "elasticSearchProperty cannot null ");
|
||||
Assert.notNull(elasticSearchProperty.getAddress(), "address hosts cannot null ");
|
||||
|
||||
//ElasticSearch 连接地址地址
|
||||
HttpHost[] httpHosts = this.getElasticSearchHttpHosts();
|
||||
|
||||
return RestClient.builder(httpHosts).setRequestConfigCallback(requestConfigBuilder -> {
|
||||
//设置连接超时时间
|
||||
requestConfigBuilder.setConnectTimeout(elasticSearchProperty.getConnectTimeout());
|
||||
requestConfigBuilder.setSocketTimeout(elasticSearchProperty.getSocketTimeout());
|
||||
requestConfigBuilder.setConnectionRequestTimeout(elasticSearchProperty.getConnectionRequestTimeout());
|
||||
return requestConfigBuilder;
|
||||
}).setFailureListener(new RestClient.FailureListener() {
|
||||
//某节点失败,这里可以添加一些异常告警
|
||||
@Override
|
||||
public void onFailure(Node node) {
|
||||
log.error("[ ElasticSearchClient ] >> node :{}, host:{}, fail ", node.getName(), node.getHost());
|
||||
}
|
||||
}).setHttpClientConfigCallback(httpClientBuilder -> {
|
||||
httpClientBuilder.disableAuthCaching();
|
||||
//设置账密
|
||||
return getHttpAsyncClientBuilder(httpClientBuilder);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* ElasticSearch Rest client 配置
|
||||
*
|
||||
* @return RestHighLevelClient
|
||||
*/
|
||||
@Bean
|
||||
public RestHighLevelClient restHighLevelClient(@Qualifier("restClientBuilder") RestClientBuilder restClientBuilder) {
|
||||
return new RestHighLevelClient(restClientBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* ElasticSearch 连接地址
|
||||
* 多个逗号分隔
|
||||
* 示例:127.0.0.1:9201,127.0.0.1:9202,127.0.0.1:9203
|
||||
*/
|
||||
private HttpHost[] getElasticSearchHttpHosts() {
|
||||
String[] hosts = elasticSearchProperty.getAddress().split(",");
|
||||
HttpHost[] httpHosts = new HttpHost[hosts.length];
|
||||
for (int i = 0; i < httpHosts.length; i++) {
|
||||
String host = hosts[i];
|
||||
host = host.replaceAll("http://", "").replaceAll("https://", "");
|
||||
Assert.isTrue(host.contains(":"), String.format("your host %s format error , Please refer to [ 127.0.0.1:9200 ] ", host));
|
||||
httpHosts[i] = new HttpHost(host.split(":")[0], Integer.parseInt(host.split(":")[1]), "http");
|
||||
}
|
||||
return httpHosts;
|
||||
}
|
||||
|
||||
private HttpAsyncClientBuilder getHttpAsyncClientBuilder(HttpAsyncClientBuilder httpClientBuilder) {
|
||||
if (StringUtils.isEmpty(elasticSearchProperty.getUserName()) || StringUtils.isEmpty(elasticSearchProperty.getPassword())) {
|
||||
return httpClientBuilder;
|
||||
}
|
||||
//账密设置
|
||||
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
||||
//es账号密码(一般使用,用户elastic)
|
||||
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(elasticSearchProperty.getUserName(), elasticSearchProperty.getPassword()));
|
||||
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
|
||||
return httpClientBuilder;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.adc.da.report.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* ElasticSearch配置
|
||||
*
|
||||
* @author 程序员小强
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "elasticsearch")
|
||||
public class ElasticSearchProperty {
|
||||
|
||||
/**
|
||||
* 连接地址,格式:IP:端口
|
||||
* 多个逗号分隔
|
||||
* 示例:127.0.0.1:9201,127.0.0.1:9202,127.0.0.1:9203
|
||||
*/
|
||||
private String address;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String userName;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 连接超时时间
|
||||
* 默认10s
|
||||
*/
|
||||
private int connectTimeout = 10000;
|
||||
|
||||
/**
|
||||
* socket超时时间
|
||||
* 默认10s
|
||||
*/
|
||||
private int socketTimeout = 10000;
|
||||
|
||||
/**
|
||||
* 请求连接超时时间
|
||||
* 默认10s
|
||||
*/
|
||||
private int connectionRequestTimeout = 10000;
|
||||
|
||||
}
|
||||
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.adc.da.report.controller;
|
||||
|
||||
import com.adc.da.report.client.ElasticSearchRestApiClient;
|
||||
import com.adc.da.report.model.ReportInfo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 程序员小强
|
||||
* @date 2020-12-14 10:21
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/elasticSearch")
|
||||
public class ElasticSearchTestController {
|
||||
|
||||
@Autowired
|
||||
private ElasticSearchRestApiClient restHighLevelClient;
|
||||
|
||||
private static final String INDEX = "test_index";
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* 使用,from-size 的"浅"分页
|
||||
*/
|
||||
@RequestMapping("searchPageByIndex")
|
||||
public Object searchPageByIndex(@RequestParam(value = "pageNo", required = false) Integer pageNo,
|
||||
@RequestParam(value = "pageSize", required = false) Integer pageSize,
|
||||
@RequestParam(value = "index", required = false) String index) {
|
||||
pageNo = pageNo == null ? 1 : pageNo;
|
||||
pageSize = pageSize == null ? 10 : pageSize;
|
||||
index = StringUtils.isEmpty(index) ? INDEX : index;
|
||||
List<ReportInfo> dataList = restHighLevelClient.searchPageByIndex(index, pageNo, pageSize, ReportInfo.class);
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("index", index);
|
||||
result.put("pageNo", pageNo);
|
||||
result.put("pageSize", pageSize);
|
||||
result.put("dataList", dataList);
|
||||
log.info("[ searchPageByIndex ] >> index:{},pageNo:{},pageSize:{}", index, pageNo, pageSize);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.adc.da.report.exception;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
|
||||
/**
|
||||
* 基础异常
|
||||
*
|
||||
* @author 程序员小强
|
||||
*/
|
||||
public class BaseException extends RuntimeException {
|
||||
|
||||
public String msg;
|
||||
|
||||
public BaseException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public BaseException(String msgFormat, Object... args) {
|
||||
super(MessageFormat.format(msgFormat, args));
|
||||
this.msg = MessageFormat.format(msgFormat, args);
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return this.msg;
|
||||
}
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.adc.da.report.exception;
|
||||
|
||||
|
||||
/**
|
||||
* es 执行异常
|
||||
*
|
||||
* @author 程序员小强
|
||||
*/
|
||||
public class ElasticSearchRunException extends BaseException {
|
||||
|
||||
public ElasticSearchRunException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ElasticSearchRunException(String mess, Object... args) {
|
||||
super(mess, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.adc.da.report.model;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 报表信息实体类
|
||||
* @author Caihaohan
|
||||
*/
|
||||
@Data
|
||||
public class ReportInfo implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 8802812229085206905L;
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
private String reportId;
|
||||
|
||||
/**
|
||||
* 文章内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
}
|
||||
@@ -28,6 +28,9 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.adc.da.report.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 分页工具类
|
||||
*
|
||||
* @author 程序员小强
|
||||
*/
|
||||
public class PageUtils {
|
||||
|
||||
/**
|
||||
* 默认第一页
|
||||
*/
|
||||
private static final int PAGE = 1;
|
||||
|
||||
/**
|
||||
* 默认一页10条
|
||||
*/
|
||||
private static final int PAGE_SIZE = 10;
|
||||
|
||||
|
||||
public static Integer getStartRow(Integer pageNo, Integer pageSize) {
|
||||
if (null == pageNo) {
|
||||
pageNo = PAGE;
|
||||
}
|
||||
if (null == pageSize) {
|
||||
pageSize = PAGE_SIZE;
|
||||
}
|
||||
|
||||
return pageSize * (pageNo - 1);
|
||||
}
|
||||
|
||||
public static Integer getOffset(Integer pageSize) {
|
||||
if (null == pageSize) {
|
||||
pageSize = PAGE_SIZE;
|
||||
}
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* list分页
|
||||
*
|
||||
* @param list
|
||||
* @param pageNo
|
||||
* @param pageSize
|
||||
*/
|
||||
public static <T> List<T> listPage(List<T> list, Integer pageNo, Integer pageSize) {
|
||||
if (null == list || list.isEmpty()) {
|
||||
return list;
|
||||
}
|
||||
if (null == pageNo) {
|
||||
pageNo = PAGE;
|
||||
}
|
||||
if (null == pageSize) {
|
||||
pageSize = PAGE_SIZE;
|
||||
}
|
||||
int totalCount = list.size();
|
||||
pageNo = pageNo - 1;
|
||||
int fromIndex = pageNo * pageSize;
|
||||
if (fromIndex > totalCount) {
|
||||
return new ArrayList<>(0);
|
||||
}
|
||||
int toIndex = ((pageNo + 1) * pageSize);
|
||||
if (toIndex > totalCount) {
|
||||
toIndex = totalCount;
|
||||
}
|
||||
return list.subList(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user