QueryBuilders.boolQuery() 条件组合查询 相当于 and QueryBuilder.matchQuery() 单个分词查询 QueryBuilder.multiMatchQuery() 指定多个字段条件匹配查询 QueryBuilder.fuzzyQuery() 模糊查询 IndicesQueryBuilder.indicesQuery() 索引查询
must 相当于 sql 中的 and must_not 相当于 sql 中的 not should 相当于sql 中的 or
match_all 查询所有
term 严格匹配条件,所查询的字段内容要与填入查询框搜索值一致,同时不对检索内容分词,因此如果查询内容长的话,不分词是不能匹配到结果的 range 范围查询数值或日期,gt为大于,gte为大于等于,lt小于,lte小于等于,所搜索的字段值在两个搜索框标识数值之间; fuzzy 是弹性模糊匹配,有两种匹配模式 query_string 字符片段查询,如果是数字,则严格匹配数字,如果是字符串,则按照自身或者分片词匹配; missing 查询没有定义该字段或者该字段值为null的数据。
private Object[] queryData(String tableName,QueryBean queryBean) throws IOException {
SearchRequest searchRequest = new SearchRequest(tableName);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("tenantId", queryBean.getTenantId()));
RangeQueryBuilder rangQueryBuilder = QueryBuilders.rangeQuery(dateName)
.from(queryBean.getCreateDate())
.to(time)
.includeUpper(false);
boolQueryBuilder.must(rangQueryBuilder );
sourceBuilder.sort(new FieldSortBuilder("createDate").order(SortOrder.DESC));
Integer pageNum = queryBean.getPage();
Integer pageSize = queryBean.getPageSize();
if (pageNum != null && pageSize != null && !isChannel) {
sourceBuilder.from((pageNum - 1) * pageSize).size(pageSize);
} else {
sourceBuilder.from(0).size(CommonConstants.ES_MAX_PAGE_NUM);
}
sourceBuilder.query(boolQueryBuilder);
log.info("查询sql:{}",sourceBuilder);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = esTemplate.getClient().search(searchRequest, RequestOptions.DEFAULT);
if (searchResponse == null || searchResponse.getHits() == null || searchResponse.getHits().getHits() == null) {
return null;
}
return searchResponse.getHits().getHits();
}
参考地址: https://www.cnblogs.com/huqi96/p/14875880.html https://blog.csdn.net/qq_35530042/article/details/106674039
|