datetime - Elasticsearch如何在日期时间字段中按时间搜索

标签 datetime elasticsearch time range elasticsearch-dsl

我有一个假想的索引test与下面的映射

PUT test
{"mappings" : {
      "properties" : {
        "endTs" : {
          "type" : "date",
          "format": "yyyy-MM-dd HH:mm:ss",
          "index": true
        },
        "startTs" : {
          "type" : "date",
          "format": "yyyy-MM-dd HH:mm:ss",
          "index": true
        }
      }
    }
}

并添加如下所示的值
POST _bulk
{ "index" : { "_index" : "test"} }
{ "startTs": "2020-01-01 00:00:00", "endTs": "2020-01-01 00:15:00" }
{ "index" : { "_index" : "test"} }
{ "startTs" : "2020-01-01 00:15:00", "endTs" : "2020-01-01 00:30:00" }
{ "index" : { "_index" : "test"} }
{ "startTs" : "2020-01-01 00:30:00", "endTs" : "2020-01-01 00:45:00" }
{ "index" : { "_index" : "test"} }
{ "startTs" : "2020-01-01 00:45:00", "endTs" : "2020-01-01 01:00:00" }
{ "index" : { "_index" : "test"} }
{ "startTs": "2020-01-01 01:00:00", "endTs": "2020-01-01 01:15:00" }
{ "index" : { "_index" : "test"} }
{ "startTs" : "2020-01-01 01:15:00", "endTs" : "2020-01-01 01:30:00" }
{ "index" : { "_index" : "test"} }
{ "startTs" : "2020-01-01 01:30:00", "endTs" : "2020-01-01 01:45:00" }
{ "index" : { "_index" : "test"} }
{ "startTs" : "2020-01-01 01:45:00", "endTs" : "2020-01-01 02:00:00" }

我的目标是在startTsendTs字段中运行一些范围查询。像startTs: 2020-01-01 00:00:00'endTs: 2020-01-01 00:45:00'。我只需要使用time而不是完整的datetime格式进行过滤。所以我尝试了下面的过滤器
GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "startTs": {
              "gte": "00:00:00",
              "format": "HH:mm:ss"
            }
          }
        },
        {
          "range": {
            "endTs": {
              "lte": "00:45:00",
              "format": "HH:mm:ss"
            }
          }
        }
      ]
    }
  }
}

我预计它将返回所有记录值,小于等于2020-01-01 00:45:00
{ "startTs": "2020-01-01 00:00:00", "endTs": "2020-01-01 00:15:00" }
{ "startTs" : "2020-01-01 00:15:00", "endTs" : "2020-01-01 00:30:00" }
{ "startTs" : "2020-01-01 00:30:00", "endTs" : "2020-01-01 00:45:00" }

但是它什么也没有返回。
"hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }

然后我尝试
GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "script": {
            "script": {
              "source": "doc['startTs'].value.minute >= 0"
            }
          }
        },
        {
          "script": {
            "script": {
              "source": "doc['endTs'].value.minute <= 45"
            }
          }
        }
      ]
    }
  }
}

它返回所有分钟小于等于45的值,这也是不期望的。

如何在时间范围内运行给定索引的查询? 非常感谢您的帮助。

我正在使用Elasticsearch 7.6

最佳答案

建议您不要使用从一天开始以来的分钟或秒数(取决于您需要的分辨率)来索引date,而不是索引integer类型。然后对该字段运行范围查询。
date类型不支持您要在此处执行的查询类型。

关于datetime - Elasticsearch如何在日期时间字段中按时间搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61499856/

相关文章:

SQL 查询不选择日期之间的所有行

search - Elasticsearch query_string与match_phrase结合

elasticsearch - Elasticsearch搜索时间一致性

time - 如何保持覆盆子计时准确?

java - 无法将时间字符串解析为指定格式

mysql - 如何使用时区信息在 MySQL 中存储日期时间

javascript - 从 d3.js timeFormat 中删除前导零

python - 如何在python中转换和减去日期,时间

python - 给定时间原点,如何将日期时间从十进制转换为 “%y-%m-%d %H:%M:%S”?

elasticsearch - 在Nest Elasticsearch中使用术语汇总功能查找特定文档