ES时间范围查询案例,并分页查询

 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;
    }

展开阅读剩余部分

es 字符串时间格式的字符串怎么做小时查询

import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;

// 创建一个脚本,用于解析时间字符串并检查小时是否等于指定的小时
Script script = new Script(
    ScriptType.INLINE,
    "painless",
    "def formatter = DateTimeFormatter.ofPattern('yyyy-MM-dd HH:mm:ss');\n" +
    "def dateTime = LocalDateTime.parse(doc['eventTime'].value, formatter);\n" +
    "if (dateTime == null) {\n" +
    "    logger.error('Failed to parse eventTime value: ' + doc['eventTime'].value);\n" +
    "}\n" +
    "return dateTime.getHour() == Integer.valueOf(params.hour);",
    Collections.singletonMap("hour", "00") // 设置参数值为字符串类型的 "00"
);

// 创建一个脚本查询,匹配指定小时的数据
QueryBuilder scriptQuery = QueryBuilders.scriptQuery(script);

// 将脚本查询与其他条件组合
QueryBuilder finalQuery = QueryBuilders.boolQuery()
    .must(QueryBuilders.termQuery("in_out_type", "1")) // 添加其他条件,例如匹配in_out_type为1的数据
    .must(scriptQuery);

// 现在你可以使用finalQuery来执行你的Elasticsearch查询

展开阅读剩余部分