我有一个查询,该查询获取我设置的日期的所有数据。到目前为止,该查询效果很好。
当我删除日期并更改格式时,我认为Elastic会占用当前日期,但不会。
**GET /BLA*/_search**
{
"size" : 1,
"sort" : "@timestamp",
"_source" : ["@timestamp", "details"],
"query" : {
"bool" : {
"must" : [
{"term" : {"FIELD" : "TRUC"}},
{"regexp" : { "details" : ".*TRUC.*" }},
{"range": {
"@timestamp": {
"format" : "yyyy-MM-dd HH:mm",
"from": "2020-05-05 07:00",
"to": "2020-05-05 07:30",
"time_zone" : "Europe/Paris"
}
}}
]
}
}
}
我试图删除日期并更改格式,但是它不起作用。一个想法?目标是获取当天的数据。
**GET /BLA*/_search**
{
"size" : 1,
"sort" : "@timestamp",
"_source" : ["@timestamp", "details"],
"query" : {
"bool" : {
"must" : [
{"term" : {"FIELD" : "TRUC"}},
{"regexp" : { "details" : ".*TRUC.*" }},
{"range": {
"@timestamp": {
"format" : "HH:mm",
"from": "07:00",
"to": "07:30",
"time_zone" : "Europe/Paris"
}
}}
]
}
}
}
最佳答案
TL; DR不可能。以下是在范围查询中唯一具有相对日期时间的way I know of:
{
"size": 1,
"sort": "@timestamp",
"_source": [
"@timestamp",
"details"
],
"query": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
"gte": "now-2h",
"lte": "now+3h",
"time_zone": "Europe/Paris"
}
}
}
]
}
}
}
但这都与now
有关。这是为什么您的查询似乎不起作用的解释。
让我们建立一个索引,其
@timestamp
的格式应为HH:mm
:PUT my_index
{
"mappings": {
"properties": {
"@timestamp": {
"type": "date",
"format": "HH:mm"
}
}
}
}
然后撷取文件POST my_index/_doc
{
"@timestamp": "07:00"
}
一切都很好,花花公子。现在让我们研究一下实际的索引日期是:GET my_index/_search
{
"script_fields": {
"ts_full": {
"script": {
"source": """
LocalDateTime.ofInstant(
Instant.ofEpochMilli(doc['@timestamp'].value.millis),
ZoneId.of('Europe/Paris')
).format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"))
"""
}
}
}
}
产生"01/01/1970 08:00:00"
因为巴黎是UTC + 1。综上所述,您当然可以按
HH:mm
格式的“日期”(严格来说是“时间”)进行索引和搜索,但它们都属于1970年1月1日。
关于elasticsearch - 当天时间弹性查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61664441/