1、引入依赖
org.projectlombok lombokorg.springframework.boot spring-boot-starter-weborg.mybatis.spring.boot mybatis-spring-boot-startermysql mysql-connector-javacom.github.xiaoymin knife4j-spring-boot-startercom.alibaba easyexcelorg.springframework.boot spring-boot-starter-data-elasticsearch
2、编写config
package com.xtech.hello.server.statistics.config;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import java.util.List;
@Configuration
@ConfigurationProperties(prefix = "springconfig.elasticsearch")
public class ElasticsearchRestClientConfig extends AbstractElasticsearchConfiguration {
private List uris;
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
// 生产、测试、UAT地址通用
.connectedTo(uris.toArray(new String[]{}))
// .connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
@Bean
public ElasticsearchRestTemplate restTemplate(RestHighLevelClient restHighLevelClient){
return new ElasticsearchRestTemplate(restHighLevelClient);
}
public List getUris() {
return uris;
}
public void setUris(List uris) {
this.uris = uris;
}
}
3、查询service及实现类
package com.xtech.hello.server.statistics.service;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
import java.util.List;
public interface IEsRestClientService {
void searchAggregations(String index, QueryBuilder queryBuilder, List aggregations,
IHandleResponse response) throws IOException;
void search(String index, SearchSourceBuilder sourceBuilder,
IHandleResponse response) throws IOException;
}
package com.xtech.hello.server.statistics.service.impl;
import com.xtech.hello.server.statistics.service.IEsRestClientService;
import com.xtech.hello.server.statistics.service.IHandleResponse;
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.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.List;
@Service
public class EsRestClientServiceImpl implements IEsRestClientService {
@Qualifier("elasticsearchClient")
@Autowired
private RestHighLevelClient restHighLevelClient;
@Override
public void searchAggregations(String index, QueryBuilder queryBuilder, List aggregations,
IHandleResponse response) throws IOException {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
aggregations.forEach(sourceBuilder::aggregation);
if (null != queryBuilder) {
sourceBuilder.query(queryBuilder);
}
// 处理10000条限制
sourceBuilder.trackTotalHits(true);
sourceBuilder.size(0);
IndicesOptions indicesOptions = IndicesOptions.fromOptions(true, true, true, false);
search(index,sourceBuilder,indicesOptions,response);
}
@Override
public void search(String index, SearchSourceBuilder sourceBuilder,
IHandleResponse response) throws IOException {
IndicesOptions indicesOptions = IndicesOptions.fromOptions(true, true, true, false);
search(index,sourceBuilder,indicesOptions,response);
}
private void search(String index, SearchSourceBuilder sourceBuilder,IndicesOptions indicesOptions,
IHandleResponse response) throws IOException {
SearchRequest rq = new SearchRequest(index);
if (indicesOptions != null) {
rq.indicesOptions(indicesOptions);
}
rq.source(sourceBuilder);
// System.out.println(sourceBuilder.toString());
SearchResponse rp = restHighLevelClient.search(rq, RequestOptions.DEFAULT);
response.handle(rp.getAggregations(), rp.getHits());
}
}



