【问题标题】:Can I force ES to return dates in epoch_millis format?我可以强制 ES 以 epoch_millis 格式返回日期吗?
【发布时间】:2019-01-29 18:57:35
【问题描述】:

我有这个字段映射

"time": {
              "type": "date",
              "format": "strict_date_optional_time||epoch_millis"
           },

我正在使用此过滤器查询文档:

"range": {
            "time": {
              "gt": "1473157500000",
              "lte": "1473158700000",
            "format": "epoch_millis"
            }

这有效并返回文档,但结果以不同的格式显示时间字段:

"time": "2016-09-06T10:25:23.678",

是否可以强制在 epoch_millis 中返回查询?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    _source 始终返回原始文档中的数据。
    理想情况下,我觉得将_source 数据转换为所需的格式以在客户端显示或以其他方式显示可能更可取和更灵活。
    但是对于上述用例,您可以使用fielddata_fields

    fielddata_fields 将以字段数据实际存储方式的格式返回字段,如果 date 字段恰好是 epoch_millis

    来自文档:

    允许为每个匹配返回一个字段的字段数据表示 字段数据字段可以在未存储的字段上工作。它的 重要的是要了解使用 fielddata_fields 参数将 导致该字段的术语被加载到内存(缓存),这 会导致更多的内存消耗。

    例子:

    post <index>/_search
    {
        "fielddata_fields": [
           "time"
        ]    
    }
    

    【讨论】:

      【解决方案2】:

      从 ES 6.5 开始,我们需要在这个特定的结构中使用 docvalue_fields,因为 fielddata_fields 已被弃用。例如。假设我们摄取了以下格式的 json 文档:

      {
        "@version": "1",
        "@timestamp": "2019-01-29T10:01:19.217Z",
        "host": "C02NTJRMG3QD",
        "message": "hello world!"
      }
      

      现在让我们使用docvalue_fields 执行以下get 查询:

      curl -X GET \
        http://localhost:9200/myindex/_search \
        -H 'Content-Type: application/json' \
        -d '{
        "query": {
          "match_all": {}
        },
        "docvalue_fields": [
          {
            "field": "@timestamp",
            "format": "epoch_millis"
          }
        ]
      }'
      

      而且,我们会得到以下响应:

      {
          "took": 15,
          "timed_out": false,
          "_shards": {
              "total": 5,
              "successful": 5,
              "skipped": 0,
              "failed": 0
          },
          "hits": {
              "total": 1,
              "max_score": 1,
              "hits": [
                  {
                      "_index": "myindex",
                      "_type": "doc",
                      "_id": "mY8OmWgBF3ZItz5TVDiL",
                      "_score": 1,
                      "_source": {
                          "@version": "1",
                          "@timestamp": "2019-01-29T10:01:19.217Z",
                          "host": "C02NTJRMG3QD",
                          "message": "hello world!"
                      },
                      "fields": {
                          "@timestamp": [
                              "1548756079217"
                          ]
                      }
                  }
              ]
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-09-04
        • 1970-01-01
        • 2018-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-26
        相关资源
        最近更新 更多