elasticsearch 之时间类型

日期类型(Date datatype)

JSON 没有日期类型,因此在 Elasticsearch 中可以表达成:

  • 日期格式化的字符串,比如: “2015-01-01” 或者 “2015/01/01 12:10:30”;
  • 毫秒级别的 long 类型
  • 秒级别的 integer 类型,

比如: 1515150699465, 1515150699;
实际上不管日期以何种格式写入,在 ES 内部都会先换成 UTC 时间并存储为 long 类型。

日期格式可以自定义,如果没有指定的话会使用以下的默认格式:
“strict_date_optional_time||epoch_millis”

date 类型的查询在内部转为 long 处理,聚合返回的结果再根据字段定义的格式转为字符串输出。

注: 日期将始终呈现为字符串,即使它们最初是在 JSON 文档中作为 long 串提供的。

日期格式自定义,如果没有格式指定,它会使用以下默认设置:

"strict_date_optional_time||epoch_millis"

如:
PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "date": {
          "type": "date" 
        }
      }
    }
  }
}

PUT my_index/_doc/1
{ "date": "2015-01-01" } 

PUT my_index/_doc/2
{ "date": "2015-01-01T12:10:30Z" } 

PUT my_index/_doc/3
{ "date": 1420070400001 } 

GET my_index/_search
{
  "sort": { "date": "asc"} 
}

注:sort 返回为数组,值均为毫秒时间戳。

多日期格式设置
PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "date": {
          "type":   "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}
设置参数
  • boost 默认 1.0
  • doc_values 默认 true
  • format 默认 strict_date_optional_time||epoch_millis
  • locale
  • ignore_malformed 是否忽略非正常格式的值,默认 false,抛出异常
  • index 是否可被查询 默认 true
  • null_value 默认值 null
  • store 默认 false
常用 format
  • epoch_millis
  • epoch_second
参考

1.https://www.elastic.co/guide/en/elasticsearch/reference/6.4/date.html
2.https://www.elastic.co/guide/en/elasticsearch/reference/6.4/mapping-date-format.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值