Basic Mapping and Data Types
Introduce the concept of mapping, how Elasticsearch infers data types, and how to define simple explicit mappings for your fields.
Basic Mapping and Data Types is a free Elasticsearch & Full Text Search Systems lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Elasticsearch & Full Text Search Systems learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Mapping: Your Data's Blueprint
When you put data into Elasticsearch, it needs to understand what kind of data each piece is. This is where mapping comes in.
Mapping is like a schema that defines the fields in your documents and their data types, such as text, numbers, or dates.
Why Mapping Matters
Think of mapping as a blueprint for your search engine. It tells Elasticsearch:
- How to store each field
- How to index (prepare for search) each field
- How each field can be searched and analyzed
Without proper mapping, your searches might not work as expected!
Dynamic Mapping: Elasticsearch's Guess
Elasticsearch is smart! If you index a document without defining a mapping first, it tries to guess the data type for each field. This is called dynamic mapping.
It's convenient for quick starts, but not always ideal for precise control over your data.
Dynamic Mapping Example
Let's see dynamic mapping in action. We'll index a document into a new index called my_books. Elasticsearch will automatically create the index and guess the field types.
PUT /my_books/_doc/1
{
"title": "The Art of Search",
"author": "Jane Doe",
"published_year": 2023,
"is_available": true,
"pages": 320
}Inspecting Dynamic Mappings
After indexing, we can retrieve the automatically generated mapping for my_books to see what data types Elasticsearch inferred for each field.
You'll see types like text, keyword, long, and boolean.
GET /my_books/_mappingLimitations of Dynamic Mapping
While handy, dynamic mapping has limits. For example, Elasticsearch might map a city name as text (for full-text search) when you really need it as a keyword (for exact filtering).
This can lead to inefficient searches or unexpected results. That's why explicit mapping is crucial.
Defining Explicit Mappings
Explicit mapping means you define the schema yourself before indexing documents. This gives you full control over how your data is stored and indexed.
You define mappings when you create an index, inside the mappings object, under properties.
PUT /my_custom_index
{
"mappings": {
"properties": {
"field_name": { "type": "data_type" }
}
}
}Common Data Types: Text & Keyword
Two fundamental types are text and keyword:
text: Used for full-text search (e.g., book descriptions). Text is analyzed, broken into words, and processed.keyword: Used for exact value matching, filtering, and sorting (e.g., product IDs, tags). Keywords are stored as-is.
Explicit Mapping Example: Text & Keyword
Here’s how you'd explicitly map a product_name for search and a product_id for exact filtering.
Notice how we create the index with the mapping before adding any documents.
PUT /products_v1
{
"mappings": {
"properties": {
"product_name": { "type": "text" },
"product_id": { "type": "keyword" }
}
}
}Quick Check: Choosing Data Types
You're building an Elasticsearch index for an e-commerce platform. Which data types would be most appropriate for the following fields to enable efficient searching and filtering?
Recap: Mastering Your Data's Structure
Great job! You've learned the basics of Elasticsearch mapping.
- Mapping defines the structure and types of your document fields.
- Dynamic mapping is Elasticsearch's auto-guessing feature.
- Explicit mapping gives you full control, letting you define types like
text(for search) andkeyword(for exact matches), and numeric types likeintegerandfloat.
Controlling your mappings is key to powerful and accurate search!
Frequently asked questions
Is the “Basic Mapping and Data Types” lesson free?
Yes — the full text of “Basic Mapping and Data Types” is free to read here on the web, and the Elasticsearch & Full Text Search Systems course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Elasticsearch & Full Text Search Systems course, upgrade to CoddyKit PRO.
What will I learn in “Basic Mapping and Data Types”?
Introduce the concept of mapping, how Elasticsearch infers data types, and how to define simple explicit mappings for your fields. You practise Elasticsearch & Full Text Search Systems with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Elasticsearch & Full Text Search Systems?
No prior experience is required. Elasticsearch & Full Text Search Systems on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Basic Mapping and Data Types” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Elasticsearch & Full Text Search Systems lesson?
Yes. Every Elasticsearch & Full Text Search Systems lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Indexing Documents into Elasticsearch
- CRUD Operations with Documents
- Basic Mapping and Data Types
- Bulk Indexing and the Bulk API