springboot 2.2.5.RELEASE
elasticSearch 6.4.3
Elasticsearch Java API的四类client连接方式TransportClient,RestClient,Jest,Spring Data Elasticsearch,由于官方推荐使用RestClient,
就学习了RestClient,
pom.xml
<!-- 高阶restClient -->
org.elasticsearch.client
elasticsearch-rest-high-level-client
6.4.3
org.elasticsearch.client
elasticsearch-rest-client
6.4.3
org.elasticsearch
elasticsearch
6.4.3
util:
@Component
public class RestClientUtils {
/**
* 高阶Rest Client
*/
private RestHighLevelClient client = null;
@PostConstruct
private void initClient() {
client = new RestHighLevelClient(RestClient.builder(new HttpHost("es服务器ip", "es服务器端口", "http")));
}
}
//-----------------------------------》Search API 主要是些复杂查询操作《------------------------------
/**
* 同步 翻页查询 布尔查询 范围查询 匹配查询
* @param keyWords 关键字
* @param startTimeStamp 开始时间
* @param endTimeStamp 结束时间
* @param indexes 索引
* @param scrollId 翻页id
* @return
* @throws IOException
*/
public String searchAllByIndexesAndKeyWord(String scrollId,String keyWords,String startTimeStamp,String endTimeStamp,String... indexes) throws IOException {
//翻页:scrollId为null,第一次搜索,则返回搜索结果和浏览位置id
//设置scroll过期时间,2分钟
//查询完后,手动显式删除scrollId
if (scrollId == null) {
SearchResponse searchResponse = client.search(configParams(indexes,keyWords,startTimeStamp,endTimeStamp), RequestOptions.DEFAULT);
return getSourceData(searchResponse);
}else {
//再次查询不再需要配置各种参数,但需要上次查询返回的scrollId
SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
//重新设置过期时间 2分钟
scrollRequest.scroll("2m");
SearchResponse searchResponse = client.scroll(scrollRequest, RequestOptions.DEFAULT);
return getSourceData(searchResponse);
}
/**
* 查询参数配置
* @param indexes
* @param keyWords
* @param startTimeStamp
* @param endTimeStamp
* @return
*/
private SearchRequest configParams( String[] indexes, String keyWords,String startTimeStamp,String endTimeStamp) {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//(1)条件一:关键字查询 termQuery(指定字段,字段中包含的内容)-->精确查询 matchQuery --》包含有要匹配的字段即可
MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("message", keyWords);
//(2)条件二:指定时间范围内查询 logdate字段的时间格式是(yyyy-MM-dd HH:mm:ss.SSS)
RangeQueryBuilder rangeQuery = new RangeQueryBuilder("@timestamp");
//QueryBuilders.boolQuery().minimumShouldMatch(0);
rangeQuery.gte(startTimeStamp);
rangeQuery.lt(endTimeStamp);
// rangeQuery.format("dd/MM/yyyy||yyyy");
//must 相当于and
sourceBuilder.query(
QueryBuilders.boolQuery()
.must(matchQuery)
.must(rangeQuery)
);
//设置为false,即为不返回报文信息
//sourceBuilder.fetchSource(false);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
SearchRequest searchRequest2 = new SearchRequest();
//index 相当于数据库
searchRequest2.indices(indexes);
searchRequest2.source(sourceBuilder);
//翻页查询设置,设置过期时间为2分钟
searchRequest2.scroll("2m");
return searchRequest2;
}
/**
* 获取返回结果中的scrollId,获取hits-->hits-->_source中的数据
* @param searchResponse
* @return
*/
private List<String> getSourceData(SearchResponse searchResponse) {
List<String> logData = new ArrayList<>();
//获取_scroll_id
String scrollId = searchResponse.getScrollId();
logData.add(scrollId);
//Retrieving SearchHits 获取结果数据
SearchHits hits = searchResponse.getHits();
long totalHits = hits.getTotalHits();
float maxScore = hits.getMaxScore();
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
// do something with the SearchHit
String index = hit.getIndex();
String type = hit.getType();
String id = hit.getId();
float score = hit.getScore();
//获取真正要查询的数据 --》方式一
String sourceAsString = hit.getSourceAsString();
logData.add(sourceAsString);
//获取真正要查询的数据 --》方式二
// Map sourceAsMap = hit.getSourceAsMap();
// String documentTitle = (String) sourceAsMap.get("title");
// List
// Map innerObject =(Map) sourceAsMap.get("innerObject");
}
return logData;
}
/**
* 异步
* 使用SearchSourceBuilder查询指定索引下的指定字段有匹配内容的文档
* @param indexes
* @param keyWords
* @param startTimeStamp
* @param endTimeStamp
* @throws IOException
*/
public void searchAllByIndexesAndKeyWordAsync( String keyWords,String startTimeStamp,String endTimeStamp,String... indexes) throws IOException {
ActionListener<SearchResponse> listener = new ActionListener<SearchResponse>() {
@Override
public void onResponse(SearchResponse searchResponse) {
//在这里进行逻辑处理
}
@Override
public void onFailure(Exception e) {
logger.error(e.toString());
}
};
client.searchAsync(configParams(indexes,keyWords,startTimeStamp,endTimeStamp),RequestOptions.DEFAULT, listener);
}
}
调用util,进行翻页查询:
public String queryByIndexesAndKeyWord(String keyWords,String startTimeStamp,String endTimeStamp,String indexes) throws IOException, ParseException {
//索引转换成数组,关键字加空格,match语法匹配多个关键字,时间格式是UTC
List<String> logData = client.searchAllByIndexesAndKeyWord(null,keyWords.replace(",", " "),TimeFormatUtils.toUTC(startTimeStamp),TimeFormatUtils.toUTC(endTimeStamp),indexes.concat("-").concat(TimeFormatUtils.nowTimeToFormat()));
while (true) {
logData = client.searchAllByIndexesAndKeyWord(logData.get(0),keyWords.replace(",", " "),TimeFormatUtils.toUTC(startTimeStamp),TimeFormatUtils.toUTC(endTimeStamp),indexes.concat("-").concat(TimeFormatUtils.nowTimeToFormat()));
//数据已取完(logData数据存放:第一个是scrollId,第二个,及之后是查询到的数据)
if (logData.size() == 1) {
return null;
}
}
}