springBoot / Cloud 整合 ES(elasticsearch ) 7.1.1
这篇文章简单的记录一下springBoot /Cloud 整合 最新版本的ES搜索引擎, 如果有需要开始安装等相关资料可以查看:
ES:
Es权威指南(中文版):
https://es.xiaoleilu.com/010_Intro/00_README.html
ES 安装
https://blog.csdn.net/andyzhaojianhui/article/details/75195296
ES java 文档:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/_maven_repository.html
下面开始对ES的整合:
首先肯定是要添加它的Maven依赖;
在这里我使用了elasticsearch-rest-high-level-client 版本
org.elasticsearch
elasticsearch
7.1.1
org.elasticsearch.client
elasticsearch-rest-client
7.1.1
org.elasticsearch.client
elasticsearch-rest-high-level-client
org.elasticsearch
elasticsearch
org.elasticsearch.client
elasticsearch-rest-client
7.1.1
在这边为啥不用官方给的
org.elasticsearch.client
transport
7.1.1
我发现里边其中好像会出现jar包不一致的问题
这个会导致你启动的时候不会出现错误,但是在查询的时候会出现
java.lang.NoSuchMethodError: org.elasticsearch.action.search.SearchRequest.isCcsMinimizeRoundtrips()Z
这个异常!
找了半天大概就是jar包不一致的问题,所以就使用了上边的Maven依赖。
下边就是ES的Config了,直接上代码:
public class ESClientSpringFactory {
public static int CONNECT_TIMEOUT_MILLIS = 1000;
public static int SOCKET_TIMEOUT_MILLIS = 30000;
public static int CONNECTION_REQUEST_TIMEOUT_MILLIS = 500;
public static int MAX_CONN_PER_ROUTE = 10;
public static int MAX_CONN_TOTAL = 30;
private static HttpHost HTTP_HOST;
private RestClientBuilder builder;
private RestClient restClient;
private RestHighLevelClient restHighLevelClient;
private static ESClientSpringFactory esClientSpringFactory = new ESClientSpringFactory();
private ESClientSpringFactory(){}
public static ESClientSpringFactory build(HttpHost httpHost,
Integer maxConnectNum, Integer maxConnectPerRoute){
HTTP_HOST = httpHost;
MAX_CONN_TOTAL = maxConnectNum;
MAX_CONN_PER_ROUTE = maxConnectPerRoute;
return esClientSpringFactory;
}
public static ESClientSpringFactory build(HttpHost httpHost,Integer connectTimeOut, Integer socketTimeOut,
Integer connectionRequestTime,Integer maxConnectNum, Integer maxConnectPerRoute){
HTTP_HOST = httpHost;
CONNECT_TIMEOUT_MILLIS = connectTimeOut;
SOCKET_TIMEOUT_MILLIS = socketTimeOut;
CONNECTION_REQUEST_TIMEOUT_MILLIS = connectionRequestTime;
MAX_CONN_TOTAL = maxConnectNum;
MAX_CONN_PER_ROUTE = maxConnectPerRoute;
return esClientSpringFactory;
}
public void init(){
builder = RestClient.builder(HTTP_HOST);
setConnectTimeOutConfig();
setMutiConnectConfig();
restClient = builder.build();
restHighLevelClient = new RestHighLevelClient(builder);
System.out.println("init factory");
}
// 配置连接时间延时
public void setConnectTimeOutConfig(){
builder.setRequestConfigCallback(requestConfigBuilder -> {
requestConfigBuilder.setConnectTimeout(CONNECT_TIMEOUT_MILLIS);
requestConfigBuilder.setSocketTimeout(SOCKET_TIMEOUT_MILLIS);
requestConfigBuilder.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT_MILLIS);
return requestConfigBuilder;
});
}
// 使用异步httpclient时设置并发连接数
public void setMutiConnectConfig(){
builder.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setMaxConnTotal(MAX_CONN_TOTAL);
httpClientBuilder.setMaxConnPerRoute(MAX_CONN_PER_ROUTE);
return httpClientBuilder;
});
}
public RestClient getClient(){
return restClient;
}
public RestHighLevelClient getRhlClient(){
return restHighLevelClient;
}
public void close() {
if (restClient != null) {
try {
restClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("close client");
}
}
@Configuration
@Slf4j
@ComponentScan(basePackageClasses=ESClientSpringFactory.class)
public class EsConfig {
@Value("${elasticSearch.host}")
private String host;
@Value("${elasticSearch.port}")
private int port;
@Value("${elasticSearch.client.connectNum}")
private Integer connectNum;
@Value("${elasticSearch.client.connectPerRoute}")
private Integer connectPerRoute;
@Bean
public HttpHost httpHost(){
return new HttpHost(host,port,"http");
}
@Bean(initMethod="init",destroyMethod="close")
public ESClientSpringFactory getFactory(){
return ESClientSpringFactory.
build(httpHost(), connectNum, connectPerRoute);
}
@Bean
@Scope("singleton")
public RestClient getRestClient(){
return getFactory().getClient();
}
@Bean
@Scope("singleton")
public RestHighLevelClient getRHLClient(){
return getFactory().getRhlClient();
}
}
接下来就是application.properties中的配置信息拉。
elasticSearch.host= 你的服务器地址
elasticSearch.port = 9200
elasticSearch.client.connectNum=10
elasticSearch.client.connectPerRoute=50
ok,初始完成,这样就可以通过
@Autowired
private RestHighLevelClient restHighLevelClient;
去使用了;
简单的举例:
QueryBuilder moreLikeThisQuery = null;
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
SearchSourceBuilder searchSourceBuilder =new SearchSourceBuilder();
searchSourceBuilder.from(0);
searchSourceBuilder.size(10000);
QueryBuilder queryBuilder = QueryBuilders.termQuery("CHATROOM_CODE",req.getChatroomCode());
if (StringUtils.isNotBlank(req.getKeyWord())){
moreLikeThisQuery = QueryBuilders.matchPhraseQuery("TXT",req.getKeyWord());
boolQueryBuilder.must(moreLikeThisQuery);
}
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("FIRST_UPDATETIME");
rangeQueryBuilder.gte(req.getFirstTime());
rangeQueryBuilder.lte(req.getLastTime());
boolQueryBuilder.must(queryBuilder);
boolQueryBuilder.must(rangeQueryBuilder);
searchSourceBuilder.query(boolQueryBuilder);
SearchRequest searchRequest = new SearchRequest(chatRoomIndex);
searchRequest.routing(chatRoomType);
searchRequest.source(searchSourceBuilder);
SearchResponse response = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
SearchHit[] searchHits = hits.getHits();
List list = new ArrayList();
if (searchHits.length > 0){
for (SearchHit hit : searchHits) {
list.add(hit.getSourceAsMap()) ;
}
}
return rr.returnSuccessResult(list,requestReport);
更多的组合搜索,全文搜索及ES的索引,删除,修改 等 都可以去官网查看文档,
Es权威指南(中文版): https://es.xiaoleilu.com/
文章评论