ES 日期

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html
joda http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html

Date datatype https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html
Mapping » Mapping parameters » format https://www.elastic.co/guide/en/elasticsearch/reference/5.1/mapping-date-format.html#strict-date-time

PUT idx
{
   "mappings": {
      "t": {
         "properties": {
            "d1": {
               "type": "date",
               "format": "yyyy-MM-dd HH:mm:ss"//自定义格式非严格校验
            },
            "d2": {
               "type": "date"//默认内置的:严格校验
            }
         }
      }
   }
}

POST idx/t
{
    "d1":"2013-07-24 00:2:10",//02可简写为2
    "d2":"2013-7-24"//07 不能简写为7 
}


异常:
    {
       "error": {
          "root_cause": [
             {
                "type": "mapper_parsing_exception",
                "reason": "failed to parse [d2]"
             }
          ],
          "type": "mapper_parsing_exception",
          "reason": "failed to parse [d2]",
          "caused_by": {
             "type": "illegal_argument_exception",
             "reason": "Invalid format: \"2013-7-24\" is malformed at \"-7-24\""
          }
       },
       "status": 400
    } 

总结:内置的几种格式是严格校验,自定义的格式是非严格校验!

若写入: 
    "date":"2013/07/24 00:00:00" //不兼容
报错:
    "type": "mapper_parsing_exception",
          "reason": "failed to parse [date]",
          "caused_by": {
             "type": "illegal_argument_exception",
             "reason": "Invalid format: \"2013/07/24 00:00:00\" is malformed at \"/07/24 00:00:00\""
          }

源码

org.elasticsearch.common.xcontent.XContentBuilder#writeValue

else if (value instanceof Date) {
            generator.writeString(
              XContentBuilder.defaultDatePrinter.print(((Date) value).getTime()));
        }

Date datatype

dates in Elasticsearch can either be:

  • strings containing formatted dates, e.g. “2015-01-01” or “2015/01/01 12:10:30”.
  • a long number representing milliseconds-since-the-epoch.
  • an integer representing seconds-since-the-epoch.

Internally, dates are converted to UTC (if the time-zone is specified) and stored as a long number representing milliseconds-since-the-epoch.

Date formats can be customised, but if no format is specified then it uses the default:

"strict_date_optional_time||epoch_millis"

 "format": "yyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"

it will accept dates with optional timestamps, which conform to the formats supported by strict_date_optional_time or milliseconds-since-the-epoch.
解决方法及问题:
1.时间输出格式,如果是默认UTC格式,时间不是我们常用的格式,而且时区问题,少了8个小时。
解决方案:直接用毫秒值,缺点为不直观。
直接设置format为你想要的格式,比如“yyyy-MM-dd HH:mm:ss” 然后存储的时候,指定格式,并且 Mapping 也是指定相同的format 。

2.存储Date,和取出来也是Date?
json库

禁用date自发现

curl -XPUT "http://localhost:9200/myindex" -d'
{
   "mappings": {
      "tweet": {
         "date_detection": false
      }
   }
}'

场景问题

1 Custom DateTimeFormat “yyyy-MM-dd HH:mm:ss” in the mapping cannot be parsed

Caused by: java.lang.IllegalArgumentException: Invalid format: “1990-10-11 00:00:00” is malformed at ” 00:00:00”
at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:873)
https://github.com/elastic/elasticsearch-hadoop/issues/624

2 插入Date类型数据时报Invalid format

是否安装预设的格式写入、是否需要URL编码?

参考:
Dynamic mappings and dates in ElasticSearch

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页