0
0
Elasticsearchquery~10 mins

Date field types in Elasticsearch - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Date field types
Define mapping with date field
Index document with date value
Elasticsearch parses date
Stores date as milliseconds since epoch
Date queries use stored format
Return matching documents
This flow shows how Elasticsearch handles date fields: defining the field, indexing documents, parsing dates, storing them internally, and querying.
Execution Sample
Elasticsearch
PUT my_index
{
  "mappings": {
    "properties": {
      "created_at": { "type": "date" }
    }
  }
}

PUT my_index/_doc/1
{
  "created_at": "2024-06-01T12:00:00Z"
}
Defines an index with a date field and indexes a document with an ISO 8601 date string.
Execution Table
StepActionInput/ConditionResult/Output
1Define index mappingField 'created_at' type=dateMapping stored with date field type
2Index documentcreated_at='2024-06-01T12:00:00Z'Date string accepted
3Parse dateParse '2024-06-01T12:00:00Z'Parsed to milliseconds since epoch (e.g. 1717243200000)
4Store dateStore parsed millisecondsDate stored internally as long integer
5Query dateSearch for documents with date rangeDocuments with matching date returned
6ExitNo more documentsIndex ready for date queries
💡 All steps complete; date field indexed and ready for queries
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
created_atundefined"2024-06-01T12:00:00Z"1717243200000 (ms)1717243200000 (stored)1717243200000 (stored)
Key Moments - 3 Insights
Why does Elasticsearch store dates as numbers instead of strings?
Elasticsearch converts date strings to milliseconds since epoch (see Step 3 in execution_table) to enable fast range queries and sorting.
What happens if the date format in the document doesn't match the expected format?
Elasticsearch will fail to parse the date and reject the document or field update (not shown in this trace but parsing is critical at Step 3).
Can you store dates in formats other than ISO 8601?
Yes, but you must specify the format in the mapping so Elasticsearch can parse it correctly during Step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'created_at' after Step 3?
AMilliseconds since epoch (e.g. 1717243200000)
BThe original date string '2024-06-01T12:00:00Z'
CA formatted date string like 'June 1, 2024'
DUndefined
💡 Hint
Check the 'Result/Output' column in Step 3 of the execution_table.
At which step does Elasticsearch store the date internally as a number?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where the date is 'stored internally as long integer' in the execution_table.
If you index a document with a date in a custom format without updating the mapping, what will happen?
AElasticsearch will parse it correctly anyway
BThe document will be rejected due to parsing error
CThe date will be stored as a string
DThe date will be ignored silently
💡 Hint
Refer to the key_moments section about date format parsing and errors.
Concept Snapshot
Date field types in Elasticsearch:
- Define date fields in mapping with "type": "date"
- Index date strings (default ISO 8601)
- Elasticsearch parses dates to milliseconds since epoch
- Stores dates as long integers internally
- Enables fast date range queries and sorting
- Custom formats require mapping update
Full Transcript
This visual trace shows how Elasticsearch handles date field types. First, you define a mapping with a date field. Then, when you index a document with a date string, Elasticsearch parses that string into milliseconds since the epoch (January 1, 1970). It stores this number internally for efficient searching and sorting. Queries on date fields use this stored format to quickly find matching documents. If the date format does not match the expected format, Elasticsearch will reject the document. Custom date formats can be supported by specifying them in the mapping. This process ensures dates are handled consistently and efficiently.