Date field types help you store and search dates easily in Elasticsearch. They let you work with times and dates like birthdays or event times.
Date field types in Elasticsearch
PUT /my_index
{
"mappings": {
"properties": {
"my_date": {
"type": "date",
"format": "yyyy-MM-dd||epoch_millis"
}
}
}
}The type must be set to date for date fields.
The format defines how dates are written and read. You can list multiple formats separated by ||.
PUT /events
{
"mappings": {
"properties": {
"event_date": {
"type": "date",
"format": "yyyy-MM-dd"
}
}
}
}PUT /logs
{
"mappings": {
"properties": {
"timestamp": {
"type": "date",
"format": "epoch_millis"
}
}
}
}PUT /mixed_dates
{
"mappings": {
"properties": {
"date_field": {
"type": "date",
"format": "yyyy-MM-dd||yyyy/MM/dd||epoch_millis"
}
}
}
}This program creates an index with a date field that accepts two formats. It adds two documents with dates in different formats. Then it searches for documents created on June 1, 2024.
PUT /my_index
{
"mappings": {
"properties": {
"created_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
}
}
}
}
POST /my_index/_doc
{
"created_at": "2024-06-01 14:30:00"
}
POST /my_index/_doc
{
"created_at": 1717252200000
}
GET /my_index/_search
{
"query": {
"range": {
"created_at": {
"gte": "2024-06-01 00:00:00",
"lte": "2024-06-02 00:00:00"
}
}
}
}Always specify the date format if your dates are not in the default ISO format.
Elasticsearch stores dates internally as milliseconds since epoch, no matter the input format.
Using multiple formats helps when your data comes from different sources.
Date fields store time and date information in Elasticsearch.
You define the format so Elasticsearch knows how to read your dates.
You can search, sort, and filter documents by date easily.
Practice
date field type in Elasticsearch?Solution
Step 1: Understand the role of field types in Elasticsearch
Field types define how data is stored and interpreted in Elasticsearch.Step 2: Identify the function of the
Thedatefield typedatefield type specifically stores time and date information for searching and sorting.Final Answer:
To store and query time and date information -> Option AQuick Check:
Date field = store time/date [OK]
- Confusing date fields with text fields
- Thinking date fields store binary data
- Assuming date fields perform calculations
Solution
Step 1: Check the field type for date
The field must havetypeset todateto store dates.Step 2: Verify the format property
Custom date formats are specified with theformatkey inside the date field definition.Final Answer:
Mapping with type 'date' and format 'yyyy-MM-dd HH:mm:ss' -> Option BQuick Check:
Date type + format key = correct mapping [OK]
- Using type 'text' or 'integer' for date fields
- Omitting the format when using custom date strings
- Placing format outside the date field definition
event_date field?
{
"mappings": {
"properties": {
"event_date": { "type": "date", "format": "yyyy/MM/dd" }
}
}
}
Document:
{
"event_date": "2024/06/15"
}Solution
Step 1: Understand the mapping format
The mapping expects dates in the format "yyyy/MM/dd" which matches the document date "2024/06/15".Step 2: Know how Elasticsearch stores dates internally
Elasticsearch converts dates to ISO 8601 format with time and timezone, so it stores "2024-06-15T00:00:00.000Z".Final Answer:
"2024-06-15T00:00:00.000Z" -> Option CQuick Check:
Input format matches mapping, stored as ISO date [OK]
- Expecting stored value to keep original format
- Thinking Elasticsearch stores raw string
- Confusing format mismatch errors
yyyy-MM-dd but indexing documents with dates like 2024/06/15 causes errors. What is the best fix?Solution
Step 1: Identify the cause of the error
The document dates use slashes/but the mapping expects dashes-, causing format mismatch errors.Step 2: Fix the mapping to accept the document date format
Update the mapping format to includeyyyy/MM/ddso Elasticsearch can parse the input dates correctly.Final Answer:
Change the mapping format to include yyyy/MM/dd -> Option AQuick Check:
Mapping format must match input date format [OK]
- Changing field type to text loses date features
- Removing format causes parsing errors if format differs
- Not updating mapping when input format changes
created_at in ISO format and due_date in dd-MM-yyyy format. How should you define the mapping to handle both correctly?Solution
Step 1: Understand the formats of each date field
created_atuses ISO format (default),due_dateuses customdd-MM-yyyyformat.Step 2: Define mapping with correct formats per field
Setcreated_atasdatewith default format, anddue_dateasdatewithformat: dd-MM-yyyy.Final Answer:
Mapping with default date for created_at and custom format for due_date -> Option DQuick Check:
Each date field needs correct format in mapping [OK]
- Using text type instead of date loses date features
- Applying wrong format to a field causes parsing errors
- Using same format for different date formats
Ready to prove it?
Pass the test and this pattern moves unknown
