You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elasticsearch日期范围查询:如何基于固定起始日计算15天后的结束日

How to Dynamically Set End Date as 15 Days After Fixed Start Date in Elasticsearch Range Query

Perfect, this is a common use case that Elasticsearch handles natively with its date math functionality—no need to pre-calculate the end date outside the query!

Here's how you can adjust your query to automatically set the end date as 15 days after your fixed start date (2019-01-01):

{
  "size": 0,
  "_source": ["dateCreated"],
  "query": {
    "bool": {
      "must": {
        "range": {
          "date_published": {
            "from": "2019-01-01",
            "to": "2019-01-01||+15d",
            "include_lower": true,
            "include_upper": true
          }
        }
      }
    }
  }
}

Breakdown of the date math expression:

  • 2019-01-01: Your fixed start date (the base date for the calculation)
  • ||: Separates the base date from the math expression (required to tell Elasticsearch you're using date math)
  • +15d: Adds 15 days to the base date (d stands for days; you can also use other units like h for hours, M for months, etc.)

This will automatically resolve to 2019-01-16 (since 2019-01-01 plus 15 days is January 16th). The include_lower and include_upper parameters are optional here (they default to true), but I included them for clarity—they ensure both the start and end dates are included in the range.

If you ever need to adjust the offset (like +30 days or -7 days), just modify the number and unit in the to field, and Elasticsearch will handle the calculation for you!

内容的提问来源于stack exchange,提问作者Asha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:44:01