gpt4 book ai didi

elasticsearch - Elasticsearch中日期格式的有效值范围(非查询范围)

转载 作者:行者123 更新时间:2023-12-03 02:00:53 25 4
gpt4 key购买 nike

有效值范围是多少?例如,是像Epoch Timestamp,还是可以添加像1536-22-05这样的值?

到目前为止,我尝试了什么?

好吧,这不是尝试的问题。我刚刚开始使用Elasticsearch,并且正在研究我的模式。对于日期部分,我想知道它是否可以比较数百年前的日期,并且无论我搜索了多少,都没有找到直接的答案。因此,我觉得应该在某个地方回答这个问题,所以我在这里问了这个问题,只是为了将它放在某个地方。

为了回答上述问题,我添加了以下值:

{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "pezeshkyab",
"_type": "test",
"_id": "1",
"_score": 1,
"_source": {
"mydate": "1369-05-09"
}
},
{
"_index": "pezeshkyab",
"_type": "test",
"_id": "2",
"_score": 1,
"_source": {
"mydate": "1379-05-09"
}
},
{
"_index": "pezeshkyab",
"_type": "test",
"_id": "3",
"_score": 1,
"_source": {
"mydate": "1990-05-09"
}
}
]
}
}

然后执行以下查询:
POST /pezeshkyab/test/_search
{
"query": {
"range": {
"mydate": {
"gt": "1359-01-01",
"lt": "1399-12-12"
}
}
}
}

并得到了:
{
"took": 74,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "pezeshkyab",
"_type": "test",
"_id": "1",
"_score": 1,
"_source": {
"mydate": "1369-05-09"
}
},
{
"_index": "pezeshkyab",
"_type": "test",
"_id": "2",
"_score": 1,
"_source": {
"mydate": "1379-05-09"
}
}
]
}
}

因此,答案很明显,我只想将答案放在某个地方,以便将来其他人进行搜索会产生一些结果。

最佳答案

问题很可能是您的“mydate”字段没有作为日期存储在ES中。

这是一个示例,该示例在ES中将“f2”显式设置为日期,这将允许正确的查询/过滤。

PUT test

PUT /test/_mapping/type1
{
"type1" : {
"properties" : {
"f1" : {"type" : "string"},
"f2" : {"type" : "date"}
}
}
}

--verify the mapping
GET test/type1/_mapping

POST test/type1
{
"f1": "record 1",
"f2": "2000-01-01"
}

POST test/type1
{
"f1": "record 2",
"f2": "1500-01-01"
}

GET test/type1/_search
{
"query": {
"filtered": {
"filter": {
"range": {
"f2": {
"gte": "1500-01-01",
"lte": "1999-01-01"
}
}
}
}
}
}

使用此示例代码可以正确返回“记录2”,而排除“记录1”。

关于elasticsearch - Elasticsearch中日期格式的有效值范围(非查询范围),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32497482/

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