Movatterモバイル変換


[0]ホーム

URL:


Loading
  1. Elastic Docs/
  2. Reference/
  3. Elasticsearch/
  4. Mapping/
  5. Field data types

Date field type

JSON doesn’t have a date data type, so dates in Elasticsearch can either be:

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

Note

Use thedate_nanos field type if a nanosecond resolution is expected.

Queries on dates are internally converted to range queries on this long representation, and the result of aggregations and stored fields is converted back to a string depending on the date format that is associated with the field.

Note

Dates will always be rendered as strings, even if they were initially supplied as a long in the JSON document.

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

"strict_date_optional_time||epoch_millis"

This means that it will accept dates with optional timestamps, which conform to the formats supported bystrict_date_optional_time or milliseconds-since-the-epoch.

For instance:

PUT my-index-000001{  "mappings": {    "properties": {      "date": {        "type": "date"      }    }  }}PUT my-index-000001/_doc/1{ "date": "2015-01-01" }PUT my-index-000001/_doc/2{ "date": "2015-01-01T12:10:30Z" }PUT my-index-000001/_doc/3{ "date": 1420070400001 }GET my-index-000001/_search{  "sort": { "date": "asc"}}
  1. Thedate field uses the defaultformat.
  2. This document uses a plain date.
  3. This document includes a time.
  4. This document uses milliseconds-since-the-epoch.
  5. Note that thesort values that are returned are all in milliseconds-since-the-epoch.
Warning

Dates will accept numbers with a decimal point like{"date": 1618249875.123456} but there are some cases (#70085) where we’ll lose precision on those dates so they should be avoided.

Multiple formats can be specified by separating them with|| as a separator. Each format will be tried in turn until a matching format is found. The first format will be used to convert themilliseconds-since-the-epoch value back into a string.

PUT my-index-000001{  "mappings": {    "properties": {      "date": {        "type":   "date",        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"      }    }  }}

The following parameters are accepted bydate fields:

doc_values
Should the field be stored on disk in a column-stride fashion, so that it can later be used for sorting, aggregations, or scripting? Acceptstrue (default) orfalse.
format
The date format(s) that can be parsed. Defaults tostrict_date_optional_time||epoch_millis.
locale
The locale to use when parsing dates since months do not have the same names and/or abbreviations in all languages. The default is ENGLISH.
ignore_malformed
Iftrue, malformed numbers are ignored. Iffalse (default), malformed numbers throw an exception and reject the whole document. Note that this cannot be set if thescript parameter is used.
index
Should the field be quickly searchable? Acceptstrue (default) andfalse. Date fields that only havedoc_values enabled can also be queried, albeit slower.
null_value
Accepts a date value in one of the configuredformat’s as the field which is substituted for any explicitnullvalues. Defaults tonull, which means the field is treated as missing. Note that this cannot be set of thescript` parameter is used.
on_script_error
Defines what to do if the script defined by thescript parameter throws an error at indexing time. Acceptsfail (default), which will cause the entire document to be rejected, andcontinue, which will register the field in the document’s_ignored metadata field and continue indexing. This parameter can only be set if thescript field is also set.
script
If this parameter is set, then the field will index values generated by this script, rather than reading the values directly from the source. If a value is set for this field on the input document, then the document will be rejected with an error. Scripts are in the same format as theirruntime equivalent, and should emit long-valued timestamps.
store
Whether the field value should be stored and retrievable separately from the_source field. Acceptstrue orfalse (default).
meta
Metadata about the field.

If you need to send dates asseconds-since-the-epoch then make sure theformat listsepoch_second:

PUT my-index-000001{  "mappings": {    "properties": {      "date": {        "type":   "date",        "format": "strict_date_optional_time||epoch_second"      }    }  }}PUT my-index-000001/_doc/example?refresh{ "date": 1618321898 }POST my-index-000001/_search{  "fields": [ {"field": "date"}],  "_source": false}

Which will reply with a date like:

{  "hits": {    "hits": [      {        "_id": "example",        "_index": "my-index-000001",        "_score": 1.0,        "fields": {          "date": ["2021-04-13T13:51:38.000Z"]        }      }    ]  }}

Synthetic source may sortdate field values. For example:

PUT idx{  "settings": {    "index": {      "mapping": {        "source": {          "mode": "synthetic"        }      }    }  },  "mappings": {    "properties": {      "date": { "type": "date" }    }  }}PUT idx/_doc/1{  "date": ["2015-01-01T12:10:30Z", "2014-01-01T12:10:30Z"]}

Will become:

{  "date": ["2014-01-01T12:10:30.000Z", "2015-01-01T12:10:30.000Z"]}

[8]ページ先頭

©2009-2026 Movatter.jp