【问题标题】:Use number field as date in Kibana date_histogram aggregation在 Kibana date_histogram 聚合中使用数字字段作为日期
【发布时间】:2018-12-13 05:44:03
【问题描述】:

我正在尝试使用 Kibana 中的 Visualize 功能来绘制每月 date_histogram 图表,该图表计算我系统中的消息数。消息类型有一个sent_at 字段,自纪元时间起存储为number。 虽然我可以用 elasticsearch 查询做到这一点

POST /_all/message/_search?size=0
{
    "aggs" : {
        "monthly_message" : {
            "date_histogram" : {
                "field" : "sent_at",
                "interval" : "month"
            }
        }
    }
}

我在 Kibana 中遇到了一个问题:No Compatible Fields: The "myindex" index pattern does not contain any of the following field types: date

有没有办法让 Kibana 使用 number 字段作为日期?

【问题讨论】:

    标签: elasticsearch kibana aggregation


    【解决方案1】:

    据我所知,Kibana 将使用索引映射来查找日期字段,如果找不到日期字段,那么 Kibana 将无法从其他数字字段中推断出一个。

    您可以做的是将另一个名为 sent_at_date 的字段添加到您的映射中,然后使用 update-by-query API 将 sent_at 字段复制到该新字段,最后重新创建您的索引模式Kibana。

    基本上是这样的:

    # 1. add a new field to your mapping
    PUT myindex/_mapping/message
    {
       "properties": {
          "sent_at_date": {
             "type": "date"
          }
       }
    }
    
    # 2. update all your documents
    POST myindex/_update_by_query
    {
       "script": {
          "source": "ctx._source.sent_at_date = ctx._source.sent_at"
       }
    }
    

    最后在 Kibana 中重新创建您的索引模式。您应该会看到一个名为 sent_at_date 类型为 date 的新字段,您可以在 Kibana 中使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多