可以使用RangeQueryBuilder来构建时间范围查询条件,具体代码如下:

import org.elasticsearch.index.query.QueryBuilders;
import java.time.Instant;

// 获取当前系统时间戳
Instant now = Instant.now();
long currentTimestamp = now.toEpochMilli();

// 获取一个月前的时间戳
Instant oneMonthAgo = now.minus(java.time.Duration.ofDays(30));
long oneMonthAgoTimestamp = oneMonthAgo.toEpochMilli();

// 构建时间范围查询条件
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("latestTs")
        .from(oneMonthAgoTimestamp)
        .to(currentTimestamp);

上述代码中,首先使用Instant类获取当前系统时间戳和一个月前的时间戳,然后使用RangeQueryBuilder构建时间范围查询条件,查询latestTs字段在一个月内的任意时间段数据

java构建es查询条件根据latestTs字段查询当前系统的时间时间戳一个月内的任意时间段数据

原文地址: https://www.cveoy.top/t/topic/fHHR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录