在 Kibana date_histogram 聚合中使用数字字段作为日期

Use number field as date in Kibana date_histogram aggregation

我正在尝试使用 Kibana 中的 Visualize 功能来绘制月度 date_histogram 图表,该图表计算我系统中的消息数量。消息类型有一个 sent_at 字段,自纪元时间起存储为 number 。 尽管我可以使用 elasticsearch query

做到这一点
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 字段作为日期?

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

您可以做的是将另一个名为 sent_at_date 的字段添加到您的映射中,然后使用按查询更新 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 中使用它。