🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Date 原文链接: [https://www.elastic.co/guide/en/elasticsearch/reference/5.4/date.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/date.html) 译文链接: [http://www.apache.wiki/pages/editpage.action?pageId=10031175](http://www.apache.wiki/pages/editpage.action?pageId=10031175) 贡献者: [张学](/display/~zhangxue),[ApacheCN](/display/~apachecn),[Apache中文网](/display/~apachechina) JSON没有date这种数据类型, 所以elasticsearch中的date可以是以下形式: * 包含格式化日期的字符串,例如 “2015-01-01”或“2015/01/01 12:10:30”。 * 代表milliseconds-since-the-epoch的长整型数。 * 代表seconds-since-the-epoch的整型数。 在内部,日期将转换为UTC(如果指定了时区),并将其存储为表示milliseconds-since-the-epoch的长整型数。 日期格式可以自定义,但如果没有指定格式,则使用默认格式: ``` "strict_date_optional_time||epoch_millis" ``` 这意味着它将接受带有可选时间戳的日期,这些日期符合strict_date_optional_time或者milliseconds-since-the-epoch所支持的格式。 例如: ``` PUT my_index { "mappings": { "my_type": { "properties": { "date": { "type": "date" #1 } } } } } PUT my_index/my_type/1 { "date": "2015-01-01" } #2 PUT my_index/my_type/2 { "date": "2015-01-01T12:10:30Z" } #3 PUT my_index/my_type/3 { "date": 1420070400001 } #4 GET my_index/_search { "sort": { "date": "asc"} #5 } ``` | 1 | 日期属性使用的默认格式。 | | 2 | 本文档使用纯文本。 | | 3 | 本文档包含了一段时间。 | | 4 | 本文档使用milliseconds-since-the-epoch. | | 5 | 请注意,返回的排序值全部以毫秒为单位。 | ### 多日期格式 可以使用||分隔多个格式 作为分隔符。 将依次尝试每种格式,直到找到匹配的格式。 第一个格式将用于将从时间的毫秒转换为字符串。 ``` PUT my_index { "mappings": { "my_type": { "properties": { "date": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } } } } ``` ### date字段的参数 **date**字段接受以下参数: | [`boost`](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/mapping-boost.html "boost") | 映射字段级查询时间提升。 接受一个浮点数,默认为**1.0**。 | | [`doc_values`](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/doc-values.html "doc_values") | 该字段是否应该以多列的方式存储在磁盘上,以便以后可以将其用于排序,聚合或脚本? 接受**true**(默认)或**false**。 | | [`format`](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/mapping-date-format.html "format") | 可以解析的日期格式。默认是`strict_date_optional_time||epoch_millis。` | | `locale` | 由于月份没有一致的名字或者缩写,locale 一般应用在日期解析上。 默认为 [`ROOT` locale](https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html#ROOT), | | [`ignore_malformed`](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/ignore-malformed.html "ignore_malformed") | 如果为真,则格式错误的数字将被忽略。 如果为**false**(默认),格式错误的数字会引发异常并拒绝整个文档。 | | [`include_in_all`](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/include-in-all.html "include_in_all") | 字段值是否应包含在**_all**字段中? 接受真或假 如果**index**设置为**false**,或者如果父对象字段将**include_in_all**设置为**false**,则默认为**false**。 否则默认为**true**。 | | [`index`](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/mapping-index.html "index") | 应该可以搜索该字段吗? 接受**true**(默认)和**false**。 | | [`null_value`](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/null-value.html "null_value") | 接受一个配置格式的日期值作为替换任何显式空值的字段。 默认为**null**,这意味着该字段被视为丢失。 | | [`store`](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/mapping-store.html "store") | 字段值是否应与**_source**字段分开存储和检索。 接受**true**或**false**(默认)。 |