Elasticsearch 搜索Search API(7.5.0)
1. search:官网Search API
A. 请求:GET / POST /<index>/_search | /_search
{
"query": {<parameters>}
}
请求参数:index是索引名称,多个以逗号分隔
parameters是DSL语句,用来搜索请求,查询DSL
B. 查询参数:
字段 | 说明 |
from | 起始值(页码) |
size | 返回的匹配数量(每页条数),默认10,注意:size = 0是特殊搜索,只返回匹配查询的文档数 |
sort | 排序 |
C. 结果参数
字段 | 说明 | ||||||||||||||
took | es执行请求时间(毫秒) | ||||||||||||||
hits |
|
D. kibana操作
2. Multi Search:官网Multi-Search API
3. Search Scroll
4. Count
A. 用于执行查询并获取查询匹配的数目
B.
C. Java实现
private long getUserEventById(String yearMonth, Long id) { BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery() .filter(QueryBuilders.termQuery("type", ReportLogType.AUTH_LOG.getType())) .mustNot(QueryBuilders.termQuery("auth.status", LogConstants.STATUS_SUCCESS)) .filter(QueryBuilders.termQuery("user.userId", id)); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(boolQueryBuilder); CountRequest countRequest = new CountRequest(String.format("%s_%s_%s", IndexName.RWS_REPORT_LOG, tenantContext.getTenantId(), yearMonth.replace("-", "."))); countRequest.source(sourceBuilder); try { // 统计身份认证异常数据 CountResponse countResponse = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT); return countResponse.getCount(); } catch (IOException e) { e.printStackTrace(); return 0; } }
可参考:ES官网 搜索Search