0Pricing
Elasticsearch & Full Text Search Systems · 课时

动态映射与显式映射

了解动态映射与显式定义映射之间的权衡,以及如何控制动态映射行为。

动态映射与显式映射 是 CoddyKit 上的免费 Elasticsearch & Full Text Search Systems 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elasticsearch & Full Text Search Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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" → text and keyword
  • 123 → long
  • true → 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 a string, 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!

常见问题解答

「动态映射与显式映射」课时是免费的吗?

是的 — 「动态映射与显式映射」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elasticsearch & Full Text Search Systems 课程的其余内容,请升级到 CoddyKit PRO。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。

「动态映射与显式映射」这节课中我会学到什么?

了解动态映射与显式定义映射之间的权衡,以及如何控制动态映射行为。 你通过在浏览器中直接运行的动手代码来练习 Elasticsearch & Full Text Search Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Elasticsearch & Full Text Search Systems 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Elasticsearch & Full Text Search Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「动态映射与显式映射」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Elasticsearch & Full Text Search Systems 课中编写并运行代码吗?

能。每节 Elasticsearch & Full Text Search Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 自定义字段映射
  2. 动态映射与显式映射
  3. 索引模板与别名
  4. 嵌套字段与对象字段类型
← 返回 Elasticsearch & Full Text Search Systems