gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:45:34 25 4
gpt4 key购买 nike

我有一个假想的索引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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com