Elasticsearch & Full Text Search Systems · 课时

映射与数据类型基础

介绍映射的概念,了解 Elasticsearch 如何推断数据类型,以及如何为字段定义简单的显式映射。

第 3 / 4 课11 个步骤

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

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

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/_mapping

Limitations 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) and keyword (for exact matches), and numeric types like integer and float.

Controlling your mappings is key to powerful and accurate search!

免费开始

用 AI 导师学习 Elasticsearch & Full Text Search Systems — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「映射与数据类型基础」课时是免费的吗?

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

「映射与数据类型基础」这节课中我会学到什么?

介绍映射的概念,了解 Elasticsearch 如何推断数据类型,以及如何为字段定义简单的显式映射。 你通过在浏览器中直接运行的动手代码来练习 Elasticsearch & Full Text Search Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

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

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

「映射与数据类型基础」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 将文档索引到 Elasticsearch
  2. 使用文档执行 CRUD 操作
  3. 映射与数据类型基础
  4. 批量建立索引与批量接口
← 返回 Elasticsearch & Full Text Search Systems