映射时Elasticsearch更改日期格式

时间:2015-02-13 16:27:59

标签: php mysql laravel elasticsearch

我正在使用Laravel和Elasticsearch。但我无法映射我的mysql时间戳,因为ES默认只支持时间戳为ISO 8601" 2015-02-02T15:30:20"

我想要制作看起来像" 2015-02-15 15:30:22"的时间戳。 -a TS没有" T"

是否可以更改ES以便允许时间戳如" 2015-02-15 15:30:22"?

2 个答案:

答案 0 :(得分:5)

在映射中设置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}

答案 1 :(得分:0)

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

# Python 3.x code to demonstrate star pattern 

# Function to demonstrate printing pattern triangle 
def triangle(n): 

  # number of spaces 
  k = 2*n - 2

  # outer loop to handle number of rows 
  for i in range(0, n): 

    # inner loop to handle number spaces 
    # values changing acc. to requirement 
    for j in range(0, k): 
        print(end=" ") 

    # decrementing k after each loop 
    k = k - 1

    # inner loop to handle number of columns 
    # values changing acc. to outer loop 
    for j in range(0, i+1): 

        # printing stars 
        print("* ", end="") 

    # ending line after each row 
    print("\r") 

 # Driver Code 
 n = 5
 triangle(n) 

然后您要做的就是

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

请记住,如果您正在查看Kibana中的数据,则默认为显示浏览器的时间。只需单击JSON表示形式,您将看到类似$unixEpochMilli = $user->created_at->timestamp * 1000; //epoch_milli is elastic default $isoTime = $user->created_at->toIso8601String(); //also a default 的内容指示存储的纪元。