📖 Scenario: You are working with Elasticsearch to store and search event data. Each event has a date field that needs to be stored correctly so you can query by date later.
🎯 Goal: Create an Elasticsearch index mapping with a date field type. Then add a document with a date value and finally query the document by date.
📋 What You'll Learn
Create an index mapping with a date field called event_date
Use the strict_date_optional_time||epoch_millis format for the date field
Index a document with event_date set to 2023-12-25T10:00:00Z
Query the document by matching the event_date field
💡 Why This Matters
🌍 Real World
Storing and searching events, logs, or any data with dates is common in many applications like calendars, monitoring systems, and analytics.
💼 Career
Understanding date field types in Elasticsearch is essential for backend developers, data engineers, and anyone working with search and analytics platforms.
Progress0 / 4 steps
1
Create index mapping with date field
Create an index called events with a mapping that defines a date field named event_date using the format strict_date_optional_time||epoch_millis. Use the PUT method and JSON body as shown.
Elasticsearch
Hint
Use PUT /events to create the index. Inside mappings, define properties with event_date as type: date and set the format.
2
Index a document with event_date
Index a document into the events index with event_date set to "2023-12-25T10:00:00Z". Use the POST method on /events/_doc with the JSON body.
Elasticsearch
Hint
Use POST /events/_doc to add a document. The JSON body must have event_date with the exact date string.
3
Query document by event_date
Write a search query on the events index to find documents where event_date matches "2023-12-25T10:00:00Z". Use the GET method on /events/_search with a match query on event_date.
Elasticsearch
Hint
Use GET /events/_search with a JSON body containing a match query on event_date with the exact date string.
4
Display search results
Print the JSON response from the search query to show the matched document with event_date"2023-12-25T10:00:00Z".
Elasticsearch
Hint
The search response JSON should include the document with event_date set to "2023-12-25T10:00:00Z".
Practice
(1/5)
1. What is the primary purpose of a date field type in Elasticsearch?
easy
A. To store and query time and date information
B. To store large text documents
C. To perform mathematical calculations
D. To store binary data
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 date field type
The date field type specifically stores time and date information for searching and sorting.
Final Answer:
To store and query time and date information -> Option A
Quick Check:
Date field = store time/date [OK]
Hint: Date fields store time info, not text or numbers [OK]
Common Mistakes:
Confusing date fields with text fields
Thinking date fields store binary data
Assuming date fields perform calculations
2. Which of the following is the correct way to define a date field with a custom format in an Elasticsearch mapping?
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 C
Quick Check:
Input format matches mapping, stored as ISO date [OK]
Hint: Input matches format, stored as ISO 8601 date [OK]
Common Mistakes:
Expecting stored value to keep original format
Thinking Elasticsearch stores raw string
Confusing format mismatch errors
4. You defined a date field with format yyyy-MM-dd but indexing documents with dates like 2024/06/15 causes errors. What is the best fix?
medium
A. Change the mapping format to include yyyy/MM/dd
B. Convert all dates to yyyy-MM-dd before indexing
C. Change the field type to text
D. Remove the format from the mapping
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 include yyyy/MM/dd so Elasticsearch can parse the input dates correctly.
Final Answer:
Change the mapping format to include yyyy/MM/dd -> Option A
Quick Check:
Mapping format must match input date format [OK]
Hint: Mapping format must match input date format exactly [OK]
Common Mistakes:
Changing field type to text loses date features
Removing format causes parsing errors if format differs
Not updating mapping when input format changes
5. You want to index documents with multiple date fields: created_at in ISO format and due_date in dd-MM-yyyy format. How should you define the mapping to handle both correctly?