gpt4 book ai didi

elasticsearch - Elasticsearch中范围查询的奇怪行为

转载 作者:行者123 更新时间:2023-12-02 22:53:36 24 4
gpt4 key购买 nike

我的问题很简单。我有一个ES索引,其中包含UNIX时间戳记updated字段。我的索引中只有测试记录(文档),而今天它们都是创建的
我有以下查询,它运行良好,并且(严格地)在执行时不返回任何结果:

GET /test_index/_search
{
"size": 1,
"query": {
"bool": {
"must": [
{
"range": {
"updated": {
"lt": "159525360"
}
}
}
]
}
},
"sort": [
{
"updated": {
"order": "desc",
"mode": "avg"
}
}
]
}
这样就可以了。但是,当我将查询中的时间戳更改为 时,我得到了多个结果!而且这些结果在 updated字段中都包含比 5000大得多的值!更令人困惑的是,我得到的结果是 updated仅在 19719999的范围内设置。因此, 150010000之类的数字表现得很正常,我看不到任何结果。下面是行为奇怪的查询。
GET /test_index/_search
{
"size": 100,
"query": {
"bool": {
"must": [
{
"range": {
"updated": {
"lt": "5000"
}
}
}
]
}
},
"sort": [
{
"updated": {
"order": "desc",
"mode": "avg"
}
}
]
}
顺便说一句,这就是我存储在该索引中的典型文档的样子:
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "V6LDyHMBAUKhWZ7lxRtb",
"_score" : null,
"_source" : {
"councilId" : 111,
"chargerId" : "15",
"unitId" : "a",
"connectorId" : "2",
"status" : 10,
"latitude" : 77.7,
"longitude" : 77.7,
"lastStatusChange" : 1596718920,
"updated" : 1596720720,
"dataType" : "recorded"
},
"sort" : [
1596720720
]
}
这是该索引的映射:
PUT /test_index/_mapping
{
"properties": {
"chargerId": { "type": "text"},
"unitId": { "type": "text"},
"connectorId": { "type": "text"},
"councilId": { "type": "integer"},
"status": {"type": "integer"},
"longitude" : {"type": "double"},
"latitude" : {"type": "double"},
"lastStatusChange" : {"type": "date"},
"updated": {"type": "date"}
}
}
有什么解释吗?

最佳答案

ES中日期字段的默认格式为
strict_date_optional_time||epoch_millis 。由于您尚未指定epoch_second,因此您的日期被错误地解析(从纪元开始就被视为毫秒)。通过运行以下脚本可以验证:

GET test_index/_search
{
"script_fields": {
"updated_pretty": {
"script": {
"lang": "painless",
"source": """
LocalDateTime.ofInstant(
Instant.ofEpochMilli(doc['updated'].value.millis),
ZoneId.of('Europe/Vienna')
).format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"))
"""
}
}
}
}
快速修复:如下更新映射:
{
...
"updated":{
"type":"date",
"format":"epoch_second"
}
}
并重新编制索引。

关于elasticsearch - Elasticsearch中范围查询的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63302069/

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