1.es的配置
pay.center.es.hostPort = 11.4.121.226:9200,11.4.121.228:9200,11.5.13.117:9200,11.4.121.227:9200,11.5.13.116:9200,11.5.13.118:9200
pay.center.es.schema = http
pay.center.es.connectTimeOut = 1000
pay.center.es.socketTimeOut = 30000
pay.center.es.connectionRequestTimeOut = 2000
pay.center.es.maxConnectNum = 200
pay.center.es.maxConnectPerRoute = 200
pay.center.es.userName = name
pay.center.es.passWord = pass
2.工具类
package com.mark.pay.common;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.elasticsearch.action.DocWriteResponse;
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.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @param
* @Desc:es操作
* @date 2021/2/26 10:01
* @return
*/
@Slf4j
@Component
public class ElasticSearchOperation {
private final RestHighLevelClient restHighLevelClient;
public ElasticSearchOperation(RestHighLevelClient restHighLevelClient) {
this.restHighLevelClient = restHighLevelClient;
}
public boolean add(String indexName, Object data, String indexId) {
if (indexName == null || data == null || indexId == null) {
return false;
}
IndexRequest request = new IndexRequest(indexName);
request.id(indexId);
request.source(JSONObject.toJSONString(data), XContentType.JSON);
try {
IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
if (indexResponse == null) {
return false;
}
return (indexResponse.getResult() == DocWriteResponse.Result.CREATED
|| indexResponse.getResult() == DocWriteResponse.Result.UPDATED);
} catch (Throwable e) {
log.error(
"ElasticSearchGateway#add's params indexName={} and data={} and indexId = {} and Exception Occurred:",
indexName, JSONObject.toJSONString(data), indexId, e);
}
return false;
}
public void query() throws IOException {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
sourceBuilder.from(0); //设置确定结果要从哪个索引开始搜索的from选项,默认为0
sourceBuilder.size(100); //设置确定搜素命中返回数的size选项,默认为10
String[] fields = new String[]{"hashCode", "orderCode", "partnerOrderCode"};
sourceBuilder.fetchSource(fields, new String[]{}); //第一个是获取字段,第二个是过滤的字段,默认获取全部
SearchRequest rq = new SearchRequest();
//索引
rq.indices("pay_center_order");
//各种组合条件
rq.source(sourceBuilder);
//请求
log.info("请求参数:{}", rq.source().toString());
SearchResponse response = restHighLevelClient.search(rq, RequestOptions.DEFAULT);
List
3.es配置
package com.zto.pay.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* @author mark
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "pay.center.es")
public class EsConfig {
private String hostPort;
private String schema;
private int connectTimeOut;
private int socketTimeOut;
private int connectionRequestTimeOut;
private int maxConnectNum;
private int maxConnectPerRoute;
private String userName;
private String password;
}
4.RestHighLevelClientConfig客户端配置
package com.mark.pay.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.elasticsearch.client.NodeSelector;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.sniff.Sniffer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
/**
* @author mark
*/
@Import(EsConfig.class)
public class RestHighLevelClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient(EsConfig esConfig) {
String[] host = esConfig.getHostPort().split(",");
HttpHost[] httpHostArray = new HttpHost[host.length];
for (int i = 0; i < host.length; ++i) {
String[] item = host[i].split(":");
HttpHost httpHost = new HttpHost(item[0].trim(), Integer.parseInt(item[1].trim()),
esConfig.getSchema());
httpHostArray[i] = httpHost;
}
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(esConfig.getUserName(), esConfig.getPassword()));
final RestClientBuilder restClientBuilder = RestClient.builder(httpHostArray)
.setRequestConfigCallback(requestConfigBuilder ->
requestConfigBuilder.setConnectTimeout(esConfig.getConnectTimeOut())
.setSocketTimeout(esConfig.getSocketTimeOut())
.setConnectionRequestTimeout(esConfig.getConnectionRequestTimeOut())
).setHttpClientConfigCallback(httpClientBuilder ->
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
.setMaxConnTotal(esConfig.getMaxConnectNum())
.setMaxConnPerRoute(esConfig.getMaxConnectPerRoute())
).setNodeSelector(NodeSelector.SKIP_DEDICATED_MASTERS);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
//启动节点嗅探功能
Sniffer.builder(restHighLevelClient.getLowLevelClient()).build();
return restHighLevelClient;
}
}
5.条件查询
public PayRecordAllVoE queryPageRecord(PayTranPageE payTranPageE) throws Exception {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
//系统名称
if (StringUtil.isNotEmpty(payTranPageE.getSystemCode())) {
TermQueryBuilder systemBuilder = QueryBuilders.termQuery("system_code", payTranPageE.getSystemCode());
boolBuilder.must(systemBuilder);
}
//场景名称
if (StringUtil.isNotEmpty(payTranPageE.getSceneCode())) {
TermQueryBuilder sceneCode = QueryBuilders.termQuery("scene_code", payTranPageE.getSceneCode());
boolBuilder.must(sceneCode);
}
//交易类型
if (StringUtil.isNotEmpty(payTranPageE.getPayTypeCode())) {
TermQueryBuilder payTypeCode = QueryBuilders.termQuery("pay_type_code", payTranPageE.getPayTypeCode());
boolBuilder.must(payTypeCode);
}
//交易状态
if (StringUtil.isNotEmpty(payTranPageE.getStatus())) {
TermQueryBuilder payStatus = QueryBuilders.termQuery("pay_status", payTranPageE.getStatus());
boolBuilder.must(payStatus);
}
//操作状态
if (StringUtil.isNotEmpty(payTranPageE.getOperaType())) {
TermQueryBuilder operaType = QueryBuilders.termQuery("opera_type", payTranPageE.getOperaType());
boolBuilder.must(operaType);
}
//支付机构
if (StringUtil.isNotEmpty(payTranPageE.getPayOrg())) {
TermQueryBuilder orgCode = QueryBuilders.termQuery("org_code", payTranPageE.getPayOrg());
boolBuilder.must(orgCode);
}
//是否灰度, 0不是 ,1是 ,其他值全部
if (!Objects.isNull(payTranPageE.getGray()) && (payTranPageE.getGray() == 0 || payTranPageE.getGray() == 1)) {
TermQueryBuilder gray = QueryBuilders.termQuery("gray", payTranPageE.getGray());
boolBuilder.must(gray);
}
//支付渠道
if (StringUtil.isNotEmpty(payTranPageE.getChannelCode())) {
TermQueryBuilder channelCode = QueryBuilders.termQuery("channel_code", payTranPageE.getChannelCode());
boolBuilder.must(channelCode);
}
//收款卡号
if (StringUtil.isNotEmpty(payTranPageE.getReceivablesAccount())) {
TermQueryBuilder receiveAccount = QueryBuilders.termQuery("receive_cust_account", payTranPageE.getReceivablesAccount());
boolBuilder.must(receiveAccount);
}
//付款卡号
if (StringUtil.isNotEmpty(payTranPageE.getPayAccount())) {
TermQueryBuilder payCustomAccount = QueryBuilders.termQuery("pay_cust_account", payTranPageE.getPayAccount());
boolBuilder.must(payCustomAccount);
}
if (!Objects.isNull(payTranPageE.getTimeType())) {
if (payTranPageE.getTimeType() == 0) {
boolBuilder.must(QueryBuilders.rangeQuery("gmt_create").from(payTranPageE.getBeginTime()).to(payTranPageE.getEndTime()));
}
if (payTranPageE.getTimeType() == 1) {
boolBuilder.must(QueryBuilders.rangeQuery("pay_date").from(payTranPageE.getBeginTime()).to(payTranPageE.getEndTime()));
}
}
//
TermsQueryBuilder orderBuilder;
if (payTranPageE.getQueryCodeType() != null && !CollectionUtils.isEmpty(payTranPageE.getQueryCodes())) {
switch (payTranPageE.getQueryCodeType()) {
case 1:
//支付中心流水号
orderBuilder = QueryBuilders.termsQuery("order_code", payTranPageE.getQueryCodes());
break;
case 2:
//第三方流水号
orderBuilder = QueryBuilders.termsQuery("pay_trade_no", payTranPageE.getQueryCodes());
break;
case 3:
//子单编号
orderBuilder = QueryBuilders.termsQuery("partner_sub_order_code", payTranPageE.getQueryCodes());
break;
case 4:
//批次号
orderBuilder = QueryBuilders.termsQuery("batch_code", payTranPageE.getQueryCodes());
break;
default:
//订单编号
orderBuilder = QueryBuilders.termsQuery("partner_order_code", payTranPageE.getQueryCodes());
boolBuilder.must(orderBuilder);
break;
}
boolBuilder.must(orderBuilder);
}
//按照更新时间排序
FieldSortBuilder fsb = SortBuilders.fieldSort("gmt_modify");
fsb.order(SortOrder.DESC);
sourceBuilder.sort(fsb);
sourceBuilder.query(boolBuilder);
sourceBuilder.trackTotalHits(true);
Map map = elasticSearchOperation.queryEs(PAY_INDEX, sourceBuilder, (payTranPageE.getPageNum() - 1) * payTranPageE.getPageSize(), payTranPageE.getPageSize(), PayOrderE.class);
log.info("查询结果:{}", JSONObject.toJSONString(map));
List payTranPageEList = (List) map.get("list");
log.info("返回给前端的数据:{}", JSONObject.toJSONString(voEList));
PayRecordAllVoE payRecordAllVoE = new PayRecordAllVoE();
payRecordAllVoE.setList(voEList);
payRecordAllVoE.setTotalRows((Long) map.get("total"));
return payRecordAllVoE;
}