Spring Boot 集成Elastic Search 6.x
Spring Data ElasticSearch目前(2018-5-22)还不支持ES 6.X版本的,故需要集成ES官方的RestHighLevelClient。
添加依赖
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.2.4</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.2.4</version>
</dependency>
配置 RestHighLevelClient
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchConfiguration extends AbstractFactoryBean {
private static final Logger LOG = LoggerFactory.getLogger(ElasticSearchConfiguration.class);
@Value("${elasticsearch.host}")
private String host;
private RestHighLevelClient restHighLevelClient;
@Override
public void destroy() throws Exception {
// 关闭Client
if (restHighLevelClient != null) {
restHighLevelClient.close();
}
}
@Override
public Class<RestHighLevelClient> getObjectType() {
return RestHighLevelClient.class;
}
@Override
public boolean isSingleton() {
return false;
}
@Override
protected Object createInstance() throws Exception {
try {
// 如果有多个节点,构建多个HttpHost
restHighLevelClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost(host, 9200, "http")));
} catch (Exception e) {
LOG.error(e.getMessage());
}
return restHighLevelClient;
}
}
application.properties
elasticsearch.host=192.168.1.17
执行查询
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Repository
public class LogDao {
private final String INDEX = "sys_logs";
private final String TYPE = "sysLog";
@Autowired
private RestHighLevelClient restHighLevelClient;
/**
* 根据traceId查询LOG记录
* @param traceId
* @return
* @throws IOException
*/
public List<SysLog> getLogByTraceId(String traceId) throws IOException {
List<SysLog> result = new ArrayList();
// term query, 查询trace字段
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("trace", traceId));
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
// 指定索引和类型
SearchRequest searchRequest = new SearchRequest(INDEX);
searchRequest.types(TYPE);
searchRequest.source(sourceBuilder);
// 执行查询
SearchResponse searchResponse = restHighLevelClient.search(searchRequest);
SearchHits hits = searchResponse.getHits();
// 查询结果
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
String timestamp = (String) sourceAsMap.get("@timestamp");
String trace = (String) sourceAsMap.get("trace");
String service = (String) sourceAsMap.get("service");
String level = (String) sourceAsMap.get("severity");
Object stackTrace = sourceAsMap.get("stack_trace");
String stack_trace = null;
if (stackTrace != null) {
stack_trace = (String) stackTrace;
}
SysLog sysLog = new SysLog(trace, stack_trace, timestamp, level, service);
result.add(sysLog);
}
return result;
}
}
此处只是简单的查询,新增/修改/删除以及复杂查询可以参考以下两个链接。
https://www.journaldev.com/18148/spring-boot-elasticsearch
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.2/java-rest-high-search.html#_using_the_searchsourcebuilder
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.2/java-rest-high-search-scroll.html