ES时间范围查询案例,并分页查询
warning:
这篇文章距离上次修改已过183天,其中的内容可能已经有所变动。
public PageInfo<BTCarGetoutLogs> queryPage(QueryVo queryParams, ProcessForm processForm, String tableName) {
int pageNum = queryParams.getPageNum().intValue();
int pageSize = queryParams.getPageSize().intValue();
List<BTCarGetoutLogs> result = Lists.newArrayList();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//并添加了多个查询条件。must 表示必须匹配的条件,mustNot 表示禁止匹配的条件,should 表示应该匹配的条件。你可以根据需要自由组合这些查询条件。
for (ConditionVo queryParam : queryParams.getQueryParams()) {
if (com.jinw.utils.StringUtils.isNotBlank(queryParam.getFieldName()) && queryParam.getFieldName().equals("PLATE_NO") &&
ObjectUtil.isNotEmpty(queryParam.getFieldValue())) {
MatchPhrasePrefixQueryBuilder matchQueryBuilder1 = QueryBuilders.matchPhrasePrefixQuery("plateNo", queryParam.getFieldValue());
// 添加到布尔查询的 should 子句中
boolQuery.must(matchQueryBuilder1);
break;
}
}
for (ConditionVo queryParam : queryParams.getQueryParams()) {
if (com.jinw.utils.StringUtils.isNotBlank(queryParam.getFieldName()) && queryParam.getFieldName().equals("CROSS_TIME") &&
ObjectUtil.isNotEmpty(queryParam.getFieldValue())) {
WildcardQueryBuilder wildcardQuery = QueryBuilders.wildcardQuery("crossTime", "*" + queryParam.getFieldValue().toString() + "*"); // 添加到布尔查询的 should 子句中
boolQuery.must(wildcardQuery);
break;
}
}
sourceBuilder.query(boolQuery);
Long count = ESUtils.selectByCount("b_t_car_getout_logs", sourceBuilder);
// 设置排序规则
sourceBuilder.sort(SortBuilders.fieldSort("crossTime").order(SortOrder.DESC));
List<Map<String, Object>> list = ESUtils.selectByPage("b_t_car_getout_logs", sourceBuilder, pageNum, pageSize);
for (Map<String, Object> map : list) {
Map<String, Object> filteredMap = map.entrySet().stream()
.filter(entry -> entry.getValue() != null && StringUtils.isNotBlank(entry.getValue().toString()) && !entry.getValue().equals("null"))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
BTCarGetoutLogs peopleInoutLogs = BeanUtil.mapToBean(filteredMap, BTCarGetoutLogs.class, false, CopyOptions.create().ignoreNullValue());
result.add(peopleInoutLogs);
}
int totalPage = (int) Math.ceil((double) count / pageSize);
PageInfo<BTCarGetoutLogs> pageInfo = new PageInfo<>(pageNum, pageSize, totalPage);
pageInfo.setRecords(result);
return pageInfo;
}