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

在Elasticsearch中如何基于键值对象的日期键执行数据过滤?

Elasticsearch 嵌套键值对象日期过滤实现方案

结论

你需要的过滤逻辑完全可以实现。你描述的key == "date" 且 value < 2021-08-28 15:43:58 或 value > 2021-08-28 15:43:58等价于匹配嵌套数组中存在键为date、值不等于指定时间的项,适配你给出的映射结构的DSL查询写法如下:

{
  "query": {
    "nested": {
      "path": "properties",
      "query": {
        "bool": {
          "filter": [
            // 匹配键为date的嵌套项
            {
              "term": {
                "properties.key": "date"
              }
            },
            // 匹配值不等于指定时间的逻辑
            {
              "bool": {
                "should": [
                  {
                    "range": {
                      "properties.value": {
                        "lt": "2021-08-28 15:43:58",
                        "format": "yyyy-MM-dd HH:mm:ss"
                      }
                    }
                  },
                  {
                    "range": {
                      "properties.value": {
                        "gt": "2021-08-28 15:43:58",
                        "format": "yyyy-MM-dd HH:mm:ss"
                      }
                    }
                  }
                ],
                "minimum_should_match": 1
              }
            }
          ]
        }
      }
    }
  }
}

补充说明

  • 由于你的properties字段是嵌套类型数组,必须使用nested查询对每个嵌套对象单独做条件匹配,避免数组扁平化后出现键值匹配错位的问题
  • 查询中显式指定format参数是为了兼容非标准日期格式,如果你的映射中properties.value已经明确设置为date类型且格式匹配,该参数可以省略
  • 如果你需要反向匹配「不存在key为date且值等于指定时间的项」的文档,只需在外层bool查询中添加must_not子句包裹上述nested查询即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 15:48:01