![cover](https://img-blog.csdnimg.cn/0d19e3d777cf4f4181bb026e5caa04ce.png)
ElasticSearch 7.X系列之:RestHighLevelClient 多条件查询
//使用 MultiSearch Api 在一个http请求中并行执行多个Search请求。public static void multiSearch() {var client = getClient();MultiSearchRequest msRequest = new MultiSearchRequest();SearchRequest request1 = new SearchRequ
·
//使用 MultiSearch Api 在一个http请求中并行执行多个Search请求。
public static void multiSearch() {
var client = getClient();
MultiSearchRequest msRequest = new MultiSearchRequest();
SearchRequest request1 = new SearchRequest("movies");
SearchSourceBuilder builder1 = new SearchSourceBuilder();
builder1.query(QueryBuilders.matchQuery("title", "you"));
request1.source(builder1);
msRequest.add(request1);
SearchRequest request2 = new SearchRequest("movies");
SearchSourceBuilder builder2 = new SearchSourceBuilder();
builder2.query(QueryBuilders.matchQuery("title", "life"));
request2.source(builder2);
msRequest.add(request2);
try {
MultiSearchResponse responses = client.msearch(msRequest, RequestOptions.DEFAULT);
Arrays.asList(responses.getResponses()).stream()
.map(item -> item.getResponse())
.map(response -> response.getHits())
.filter(hit -> hit != null)
.forEach(hit -> hit.forEach(System.out::println));
} catch (IOException e) {
e.printStackTrace();
}finally {
close(client);
}
}
/**
* 关闭客户端
*
* @param client
*/
private static void close(RestHighLevelClient client) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取客户端
*
* @return
*/
private static RestHighLevelClient getClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("elk-node01", 9200, "http"), new HttpHost("elk-node02", 9200, "http"),
new HttpHost("elk-node03", 9200, "http")));
return client;
}
-----------------------------------------------------------------------------------
//多条件,组合时间范围查询
// 创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost(esip, 9200)));
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
TermQueryBuilder termQueryBuilder=QueryBuilders.termQuery("L7_PROTO_NAME", L7ProtoName);
boolBuilder.must(termQueryBuilder);
TermQueryBuilder termQueryBuilder=QueryBuilders.termQuery("IPV4_SRC_ADDR", Ipv4SrcAddr);
boolBuilder.must(termQueryBuilder);
RangeQueryBuilder rangeQueryBuilder=QueryBuilders.rangeQuery(eventtype).gte(gte).lte(lte).format("yyyy-MM-dd'T'HH:mm:ss+0800");
boolBuilder.must(rangeQueryBuilder);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
MultiSearchRequest request = new MultiSearchRequest();
SearchRequest firstSearchRequest = new SearchRequest();
searchSourceBuilder.from(pagebegin);
searchSourceBuilder.size(num);
searchSourceBuilder.sort(eventtype,SortOrder.DESC);
searchSourceBuilder.query(boolBuilder);
firstSearchRequest.source(searchSourceBuilder);
firstSearchRequest.indices(indices);
request.add(firstSearchRequest);
List<Object> datalist = new LinkedList<Object>();
try {
MultiSearchResponse response = esClient.msearch(request, RequestOptions.DEFAULT);
response.forEach(t->{
SearchResponse resps = t.getResponse();
SearchHits hits = resps.getHits();
resp.put("time", resps.getTook().getStringRep());
resp.put("total", hits.getTotalHits().value);
Arrays.stream(resps.getHits().getHits())
.forEach(i -> {
String socketString = i.getSourceAsString();;
JSONObject a = JSONObject.parseObject(socketString);
datalist.add(a);
});
});
esClient.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (esClient!=null) {
try {
esClient.close();
} catch (Exception e2) {
}
}
}
---------------------------------------------------
//多条件查询
// 创建es客户端
RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(new HttpHost(esip, 9200)));
//查询条件
MultiSearchRequest request = new MultiSearchRequest();
SearchRequest firstSearchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
if (dstip.length()!=0) {
searchSourceBuilder.query(QueryBuilders.termQuery("dstip", dstip));
}
if (srcip.length()!=0) {
searchSourceBuilder.query(QueryBuilders.termQuery("srcip", srcip));;
}
searchSourceBuilder.from(pagebegin);
searchSourceBuilder.size(num);
firstSearchRequest.source(searchSourceBuilder);
firstSearchRequest.indices(indices);
request.add(firstSearchRequest);
List<Map<Object, Object>> dataList = new ArrayList<Map<Object, Object>>();
try {
MultiSearchResponse response = esClient.msearch(request, RequestOptions.DEFAULT);
response.forEach(t->{
SearchResponse resps = t.getResponse();
SearchHits hits = resps.getHits();
resp.put("time", resps.getTook().getStringRep());
resp.put("total", hits.getTotalHits().value);
Arrays.stream(resps.getHits().getHits())
.forEach(i -> {
String socketString = i.getSourceAsString();;
JSONObject a = JSONObject.parseObject(socketString);
datalist.add(a);
});
});
esClient.close();
} catch (Exception e) {
//e.printStackTrace();
}finally {
if (esClient!=null) {
try {
esClient.close();
} catch (Exception e2) {
}
}
}
更多推荐
所有评论(0)