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
