liveBook · Manning

Date format

As explained in Elasticsearch in Action, Second Edition

A date format specifies the textual or numeric representation that Elasticsearch accepts and interprets as a date.

A date format tells Elasticsearch how to parse and index a date value. Flight documents can provide dates as strings such as "2021-08-06" or "2021-08-06T05:30:00", and Elasticsearch stores them as date values when the format matches the mapping. section 4.5.3

Choose the format deliberately when defining mappings:

  • Without an explicit date mapping, Elasticsearch accepts yyyy-MM-dd and yyyy/MM/dd by default. section 4.2.2 section 4.5.3
  • With an explicit mapping, incoming values must match the declared format, such as dd-MM-yyyy or dd-MM-yy. section 4.5.3
  • A date field can also accept epoch seconds, epoch milliseconds, or several formats separated by ||. section 4.5.3
  • Incorrect dynamic detection can leave dates as text, preventing date math, sorting, filtering, and aggregations. section 4.2.2

Accepting custom flight dates

PUT flights { "mappings": { "properties": { "departure_date_time": { "type": "date", "format": "dd-MM-yyyy||dd-MM-yy" } } } } This mapping accepts values such as "06-08-2021" and "06-08-21". Define the mapping before indexing documents so the expected representation is explicit. section 4.5.3

"range": {                           #1
  "departure_date_time": {
    "gte": "2021-08-06T05:00:00",    #2
    "lte": "2021-08-06T05:30:00"
    }
}

Use a range query to retrieve flights scheduled between two date-time boundaries. section 4.5.3

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains defaults, custom patterns, epoch values, multiple formats, and range queries; defers the full set of date options.
Examples
Uses flight departure timestamps, ISO and slash-separated dates, custom day-month formats, epoch values, and time ranges.
What it assumes
Assumes mapping-definition basics while teaching that JSON has no native date type and date values commonly arrive as strings.
What you can do after
Build it from scratch by defining date mappings that accept the intended string, epoch, or multiple date formats.
How it connects
Connects date formats with dynamic mapping, explicit mapping, date fields, date math, sorting, filtering, aggregations, and range queries.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.