Pemetaan Dinamis vs. Eksplisit
Pahami kompromi antara pemetaan dinamis dan pemetaan yang ditentukan secara eksplisit, serta cara mengendalikan perilaku pemetaan dinamis.
Pemetaan Dinamis vs. Eksplisit adalah pelajaran Elasticsearch & Full Text Search Systems gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Elasticsearch & Full Text Search Systems, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Elasticsearch & Full Text Search Systems mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
Mappings: Schema for Data
When you put data into Elasticsearch, it needs to know what kind of data each field holds. This definition is called a mapping.
Think of a mapping as the schema for your documents, similar to a table schema in a traditional database.
There are two main ways Elasticsearch handles mappings: dynamic (automatic) and explicit (manual).
Dynamic Mapping: Auto-Schema
Dynamic mapping is Elasticsearch's default behavior. It's like an intelligent assistant that tries to guess the data type of new fields automatically.
When you index a document with a new field that Elasticsearch hasn't seen before, it analyzes the field's value and assigns a default mapping to it.
How Dynamic Mapping Works
The magic happens with the first document containing a new field. Elasticsearch inspects the value and infers its type:
"Hello"→textandkeyword123→longtrue→boolean"2023-01-01"→date
This makes getting started very quick!
Dynamic Mapping: Benefits
Dynamic mapping offers several advantages:
- Ease of Use: No need to pre-define your schema. Just index documents!
- Flexibility: Easily adapt to changing data structures or add new fields without modifying existing mappings.
- Rapid Prototyping: Great for initial data exploration and quick development cycles.
Dynamic Mapping: Drawbacks
While convenient, dynamic mapping has downsides, especially in production:
- Inconsistent Types: A field might be mapped as a
long, then later you index astring, leading to errors. - Performance Overhead: Inferring mappings takes resources. Too many unique fields can strain the cluster.
- Schema Drift: Unintended fields can be indexed, cluttering your schema and potentially causing issues.
Explicit Mapping: Taking Control
Explicit mapping means you manually define the schema for your index before you add any documents.
You tell Elasticsearch exactly what type each field should be, how it should be analyzed, and other specific settings.
Explicit Mapping: Benefits
For production systems, explicit mapping is usually preferred:
- Data Consistency: Guarantees fields always have the correct type.
- Optimized Performance: Knowing field types upfront allows Elasticsearch to store and search data more efficiently.
- Error Prevention: Prevents unexpected data types or schema changes from breaking your application.
- Full Control: Fine-tune every aspect of how your data is handled.
Controlling Dynamic Behavior
You can control how Elasticsearch handles new fields using the dynamic setting within your mapping. It can be set to:
true(default): New fields are added dynamically.false: New fields are completely ignored.strict: New fields throw an error, preventing indexing.
This setting can be applied at the index level or for specific object fields.
Example: `dynamic: false` (Ignore)
Setting "dynamic": "false" tells Elasticsearch to completely ignore any new fields that appear in documents. They won't be indexed or searchable.
This is useful if you want to prevent accidental schema changes but don't want to fail document indexing.
PUT /my_product_index
{
"mappings": {
"dynamic": "false",
"properties": {
"product_id": { "type": "keyword" },
"name": { "type": "text" }
}
}
}
PUT /my_product_index/_doc/1
{
"product_id": "PROD001",
"name": "Laptop",
"color": "Silver"
}
// The 'color' field will be ignored and not indexed.Example: `dynamic: strict` (Error)
When "dynamic": "strict", any document containing a field not explicitly defined in the mapping will cause an indexing error.
This is the strictest approach, ensuring your schema is always exactly what you defined.
PUT /my_strict_index
{
"mappings": {
"dynamic": "strict",
"properties": {
"user_id": { "type": "keyword" },
"username": { "type": "text" }
}
}
}
PUT /my_strict_index/_doc/1
{
"user_id": "U001",
"username": "Alice",
"email": "alice@example.com"
}
// This will return an error because 'email' is new.Test Your Knowledge!
Consider an index with the following mapping:
PUT /my_data
{
"mappings": {
"dynamic": "false",
"properties": {
"id": { "type": "keyword" },
"value": { "type": "long" }
}
}
}What happens if you try to index the following document?
PUT /my_data/_doc/1
{
"id": "A1",
"value": 100,
"new_field": "extra data"
}Recap: Dynamic vs. Explicit
You've learned about the two main approaches to mapping in Elasticsearch:
- Dynamic Mapping: Automatic, flexible, great for quick starts but can lead to inconsistencies.
- Explicit Mapping: Manual, controlled, crucial for production for data integrity and performance.
The dynamic setting (true, false, strict) gives you fine-grained control over how Elasticsearch reacts to new fields. Choose wisely based on your project's needs!
Belajar Elasticsearch & Full Text Search Systems dengan tutor AI — gratis
Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.
- Kursus
- 12
- Pelajaran
- 48
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Pemetaan Dinamis vs. Eksplisit” gratis?
Ya — teks lengkap “Pemetaan Dinamis vs. Eksplisit” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Elasticsearch & Full Text Search Systems, upgrade ke CoddyKit PRO. Kursus Elasticsearch & Full Text Search Systems mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Pemetaan Dinamis vs. Eksplisit”?
Pahami kompromi antara pemetaan dinamis dan pemetaan yang ditentukan secara eksplisit, serta cara mengendalikan perilaku pemetaan dinamis. Kamu berlatih Elasticsearch & Full Text Search Systems dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Elasticsearch & Full Text Search Systems?
Tidak diperlukan pengalaman sebelumnya. Elasticsearch & Full Text Search Systems di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.
Berapa lama pelajaran “Pemetaan Dinamis vs. Eksplisit” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Elasticsearch & Full Text Search Systems ini?
Ya. Setiap pelajaran Elasticsearch & Full Text Search Systems menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Menyesuaikan Pemetaan Bidang
- Pemetaan Dinamis vs. Eksplisit
- Templat Indeks dan Alias
- Tipe Bidang Nested dan Object