【问题标题】:Elasticsearch change date format when mapping映射时Elasticsearch更改日期格式
【发布时间】:2018-12-19 13:16:55
【问题描述】:

我将 Laravel 与 Elasticsearch 结合使用。但我无法映射我的 mysql 时间戳,因为默认情况下 ES 仅支持时间戳为 ISO 8601 "2015-02-02T15:30:20"

我想映射看起来像“2015-02-15 15:30:22”的时间戳 - 一个没有“T”的 TS

是否可以更改 ES,使其允许像“2015-02-15 15:30:22”这样的时间戳?

【问题讨论】:

  • 把你的代码?可以用 PHP 的 str_replace 还是 MySQL 的 date_format 吗?
  • 在 ES 中绝对可以使用custom date formats。您只需在映射中指定模式。不过,我不知道如何使用 Laravel 做到这一点。
  • 下面的答案奏效了。我将此包与 laravel github.com/adamfairholm/Elasticquent 一起使用 - 但我不确定它是否也支持嵌套对象

标签: php mysql laravel elasticsearch


【解决方案1】:

在您的映射中设置custom date format,以便它接受 MySQL 格式:

curl -XPUT "http://localhost:9200/test/date-format/_mapping" -d'
{
    "date-format" : {
       "properties": {
          "timestamp": {"type": "date","index": "not_analyzed","format" : "yyyy-MM-DD HH:mm:ss"}
        }
    }
}'

然后发帖测试一下:

curl -XPOST "http://localhost:9200/test/date-format" -d'
{
      "timestamp": "2014-10-22 20:03:12"
}'

它应该返回:

{"_index":"test","_type":"test-date","_id":"AUuD22Ls9cHgxNzY1tfJ","_version":1,"created":true}

【讨论】:

  • Olly,知道如何强制支持 0000-00-00 00:00:00 默认时间戳吗?
【解决方案2】:

Laravel 时间戳通常是 Carbon 实例,因此只需将它们转换为您的 accepted format

{
  "date-format": {
    "properties": {
      "timestamp": {
        "type": "date",
        "format": "strict_date_optional_time|| epoch_milli"   //default 
      }
    }
  }
}

那么你需要做的就是

$unixEpochMilli = $user->created_at->timestamp * 1000;  //epoch_milli is elastic default
$isoTime = $user->created_at->toIso8601String();   //also a default

请记住,如果您在 Kibana 中查看数据,则默认使用浏览器时间进行显示。只需单击 JSON 表示,您就会看到类似 2018-12-19T05:14:21.000Z 的内容,表示存储的纪元。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 2015-12-04
    • 2012-09-09
    • 1970-01-01
    • 2016-12-11
    • 2011-08-12
    相关资源
    最近更新 更多