feat(商城): 添加elasticsearch
parent
605a86ac22
commit
95e15ee4ef
@ -0,0 +1,39 @@
|
||||
package com.wayn.mobile.framework.config;
|
||||
|
||||
import org.apache.http.HttpHost;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestClientBuilder;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ElasticConfig {
|
||||
@Value("${es.host}")
|
||||
public String host;
|
||||
@Value("${es.port}")
|
||||
public int port;
|
||||
@Value("${es.scheme}")
|
||||
public String scheme;
|
||||
|
||||
@Bean
|
||||
public RestClientBuilder restClientBuilder() {
|
||||
return RestClient.builder(makeHttpHost());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RestClient elasticsearchRestClient() {
|
||||
return RestClient.builder(new HttpHost(host, port, scheme)).build();
|
||||
}
|
||||
|
||||
private HttpHost makeHttpHost() {
|
||||
return new HttpHost(host, port, scheme);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RestHighLevelClient restHighLevelClient(@Autowired RestClientBuilder restClientBuilder) {
|
||||
return new RestHighLevelClient(restClientBuilder);
|
||||
}
|
||||
}
|
@ -0,0 +1,210 @@
|
||||
package com.wayn.mobile.framework.manager.service;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.wayn.common.base.ElasticEntity;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
||||
import org.elasticsearch.action.bulk.BulkRequest;
|
||||
import org.elasticsearch.action.delete.DeleteRequest;
|
||||
import org.elasticsearch.action.index.IndexRequest;
|
||||
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.indices.CreateIndexRequest;
|
||||
import org.elasticsearch.client.indices.CreateIndexResponse;
|
||||
import org.elasticsearch.client.indices.GetIndexRequest;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class BaseElasticService {
|
||||
|
||||
@Autowired
|
||||
RestHighLevelClient restHighLevelClient;
|
||||
|
||||
/**
|
||||
* 创建索引
|
||||
*
|
||||
* @param idxName 缩影名称
|
||||
* @param idxSQL 缩影定义
|
||||
*/
|
||||
public void createIndex(String idxName, String idxSQL) {
|
||||
try {
|
||||
if (this.indexExist(idxName)) {
|
||||
log.error(" idxName={} 已经存在,idxSql={}", idxName, idxSQL);
|
||||
return;
|
||||
}
|
||||
CreateIndexRequest request = new CreateIndexRequest(idxName);
|
||||
buildSetting(request);
|
||||
request.mapping(idxSQL, XContentType.JSON);
|
||||
// request.settings() 手工指定Setting
|
||||
CreateIndexResponse res = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
|
||||
if (!res.isAcknowledged()) {
|
||||
throw new RuntimeException("初始化失败");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 断某个index是否存在
|
||||
*
|
||||
* @param idxName 索引名称
|
||||
* @return boolean
|
||||
* @throws Exception 异常
|
||||
*/
|
||||
public boolean indexExist(String idxName) throws Exception {
|
||||
GetIndexRequest request = new GetIndexRequest(idxName);
|
||||
request.local(false);
|
||||
request.humanReadable(true);
|
||||
request.includeDefaults(false);
|
||||
request.indicesOptions(IndicesOptions.lenientExpandOpen());
|
||||
return !restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 断某个index是否存在
|
||||
*
|
||||
* @param idxName index名
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean isExistsIndex(String idxName) throws Exception {
|
||||
return restHighLevelClient.indices().exists(new GetIndexRequest(idxName), RequestOptions.DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置分片
|
||||
*
|
||||
* @param request
|
||||
*/
|
||||
public void buildSetting(CreateIndexRequest request) {
|
||||
request.settings(Settings.builder().put("index.number_of_shards", 3)
|
||||
.put("index.number_of_replicas", 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param idxName index
|
||||
* @param entity 对象
|
||||
*/
|
||||
public void insertOrUpdateOne(String idxName, ElasticEntity entity) {
|
||||
IndexRequest request = new IndexRequest(idxName);
|
||||
log.error("Data : id={},entity={}", entity.getId(), JSON.toJSONString(entity.getData()));
|
||||
request.id(entity.getId());
|
||||
request.source(entity.getData(), XContentType.JSON);
|
||||
// request.source(JSON.toJSONString(entity.getData()), XContentType.JSON);
|
||||
try {
|
||||
restHighLevelClient.index(request, RequestOptions.DEFAULT);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 批量插入数据
|
||||
*
|
||||
* @param idxName index
|
||||
* @param list 带插入列表
|
||||
*/
|
||||
public void insertBatch(String idxName, List<ElasticEntity> list) {
|
||||
BulkRequest request = new BulkRequest();
|
||||
list.forEach(item -> request.add(new IndexRequest(idxName).id(item.getId())
|
||||
.source(item.getData(), XContentType.JSON)));
|
||||
try {
|
||||
restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除
|
||||
*
|
||||
* @param idxName index
|
||||
* @param idList 待删除列表
|
||||
*/
|
||||
public <T> void deleteBatch(String idxName, Collection<T> idList) {
|
||||
BulkRequest request = new BulkRequest();
|
||||
idList.forEach(item -> request.add(new DeleteRequest(idxName, item.toString())));
|
||||
try {
|
||||
restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param idxName index
|
||||
* @param builder 查询参数
|
||||
* @param c 结果类对象
|
||||
* @return java.util.List<T>
|
||||
*/
|
||||
public <T> List<T> search(String idxName, SearchSourceBuilder builder, Class<T> c) {
|
||||
SearchRequest request = new SearchRequest(idxName);
|
||||
request.source(builder);
|
||||
try {
|
||||
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
|
||||
SearchHit[] hits = response.getHits().getHits();
|
||||
List<T> res = new ArrayList<>(hits.length);
|
||||
for (SearchHit hit : hits) {
|
||||
res.add(JSON.parseObject(hit.getSourceAsString(), c));
|
||||
}
|
||||
return res;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除index
|
||||
*
|
||||
* @param idxName 索引名称
|
||||
*/
|
||||
public void deleteIndex(String idxName) {
|
||||
try {
|
||||
if (this.indexExist(idxName)) {
|
||||
log.error(" idxName={} 已经存在", idxName);
|
||||
return;
|
||||
}
|
||||
restHighLevelClient.indices().delete(new DeleteIndexRequest(idxName), RequestOptions.DEFAULT);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据查询条件删除文档
|
||||
*
|
||||
* @param idxName 缩影名称
|
||||
* @param builder 查询条件
|
||||
*/
|
||||
public void deleteByQuery(String idxName, QueryBuilder builder) {
|
||||
|
||||
DeleteByQueryRequest request = new DeleteByQueryRequest(idxName);
|
||||
request.setQuery(builder);
|
||||
//设置批量操作数量,最大为10000
|
||||
request.setBatchSize(10000);
|
||||
request.setConflicts("proceed");
|
||||
try {
|
||||
restHighLevelClient.deleteByQuery(request, RequestOptions.DEFAULT);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue