elasticsearch - 当天时间弹性查询

标签 elasticsearch

我有一个查询,该查询获取我设置的日期的所有数据。到目前为止,该查询效果很好。
当我删除日期并更改格式时,我认为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/

相关文章:

search - 多字段,多词,不带query_string的匹配

spring - 删除 Elasticsearch 响应中doc_count为零的存储桶

javascript - 包含来自 elasticsearch 的大数据的 HTML 表格

elasticsearch - 如何在同一请求中使用不同的查询词查询两个不同的字段。 ElasticSearch 5.x

java - 如何在Java Elasticsearch中设置超时

elasticsearch - Apache Nifi使用_Parent将数据放入ElasticSearch

elasticsearch - logstash|elasticsearch - 维护原始插入序列

elasticsearch - Kibana 4 'Discover'搜索错误

elasticsearch - Elasticsearch中的SpanWithInQuery和SpanContainingQuery有什么区别?

c# - NEST 5.5属性映射和自定义JsonConverter不起作用