liveBook · Manning

Date field

As explained in Elasticsearch in Action, Second Edition

A field that stores values interpreted as dates or times.

A date field stores values that Elasticsearch interprets as dates or times. Because dates are structured data, they support filtering, sorting, range queries, and aggregations rather than full-text relevance scoring. section 4.5.3 section 8.9

Define and provide dates consistently:

  • Create the field with "type": "date" in the index mapping. section 4.5.3
  • Send dates as strings such as 2021-08-06 or 2021-08-06T05:30:00; Elasticsearch parses and indexes them. section 4.5.3
  • Customize accepted formats, including alternatives such as dd-MM-yyyy, dd/MM/yyyy, and yyyy-MM-dd. section 4.5.3
  • Accept epoch values by mapping dates as epoch_millis or epoch_second; Elasticsearch stores dates internally as millisecond-based long values. section 4.5.3

Find flights within a departure window

A range query can retrieve flights departing between 05:00 and 05:30 on August 6, 2021. The gte and lte bounds define the time window, making the date field useful for operational search. section 4.5.3

Listing 4.17 Creating an index with a date type
PUT flights
{
  "mappings": {
    "properties": {
      "departure_date_time":{
        "type": "date"
      }
    }
  }
}

Inspect the mapping that creates departure_date_time as a date field before documents are indexed. section 4.5.3

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains mapping, accepted formats, epoch values, and range queries, but does not descend into date parsing internals.
Examples
Uses news publication dates, flight departure timestamps, custom date strings, epoch values, and bounded departure windows.
What it assumes
Assumes JSON documents and introduces mapping as the mechanism that assigns Elasticsearch data types.
What you can do after
Apply via Query DSL by indexing, filtering, sorting, aggregating, and range-searching date values.
How it connects
Connects date fields with mapping, structured data, range queries, sorting, filtering, aggregations, and Query DSL.

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.