首页 > 解决方案 > elasticsearch date range 获取今天和昨天之间的日期范围

问题描述

我正在使用弹性搜索,我正在尝试从前一天的任何第二个 10:am到 10:am the present-day获取报告。请帮忙,谢谢。

我试过这个:

  "@timestamp":{
    "gte": "now-1d/d+10h",
    "lt": "now/d+10h"

标签: elasticsearch

解决方案


您需要@timestamp根据您使用的数据格式设置字段的映射。你可以参考这个官方文档来了解更多关于日期字段类型的信息。

添加带有索引数据、映射、搜索查询和搜索结果的工作示例

索引映射:

{
  "mappings": {
    "properties": {
      "@timestamp": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      }
    }
  }
}

指数数据:

{
  "@timestamp": "2021-05-04 10:11:51"
}
{
  "@timestamp": "2021-05-04 09:11:51"
}
{
  "@timestamp": "2021-05-05 09:11:51"
}
{
  "@timestamp": "2022-05-05 09:11:51"
}

搜索查询:

{
  "query": {
    "range": {
      "@timestamp": {
        "gte": "now-1d/d+10h",
        "lt": "now/d+10h"
      }
    }
  }
}

搜索结果:

 "hits": [
      {
        "_index": "67392888",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "@timestamp": "2021-05-05 09:11:51"
        }
      },
      {
        "_index": "67392888",
        "_type": "_doc",
        "_id": "4",
        "_score": 1.0,
        "_source": {
          "@timestamp": "2021-05-04 10:11:51"
        }
      }
    ]

推荐阅读