自定义字段映射
深入学习为各种字段类型定义显式映射,包括文本、关键词、数值、日期和布尔字段。
自定义字段映射 是 CoddyKit 上的免费 Elasticsearch & Full Text Search Systems 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elasticsearch & Full Text Search Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Customize Mappings?
Elasticsearch is smart! It often guesses your data types when you index a document (this is called dynamic mapping). But sometimes, you need more precise control.
Explicit mappings let you define exactly how each field in your documents should be stored and indexed. This is crucial for optimal search behavior, efficient storage, and accurate aggregations.
Defining Your Index's Blueprint
A mapping acts like a schema for your index. You typically define it when you create a new index. It lives within the "mappings" object in your index creation request.
Here's the basic structure:
PUT /my_new_index
{
"mappings": {
"properties": {
"your_field_name": {
"type": "field_type_here"
}
}
}
}The "properties" object holds all your field definitions.
The 'text' Field Type
The text field type is designed for full-text search. Think of blog post content, product descriptions, or comments.
When you index data into a text field, Elasticsearch "analyzes" it:
- Breaks it into individual words (tokens).
- Converts words to lowercase.
- Removes common words (stop words) if configured.
This process makes text highly searchable but means it's not suitable for exact matching, filtering, or sorting.
The 'keyword' Field Type
The keyword field type is for exact values that should remain as-is, without analysis. Use it when you need precise matching, filtering, or sorting.
Examples of data suitable for keyword fields:
- Product IDs (e.g., "PROD-123")
- Tags (e.g., "new_arrival")
- Usernames (e.g., "john_doe")
- Status codes (e.g., "active", "pending")
keyword fields are very efficient for aggregations and exact filters.
'text' vs. 'keyword' Example
Let's illustrate the difference. Imagine indexing a blog post with a title and a tag:
PUT /my_blog_posts
{
"mappings": {
"properties": {
"title": { "type": "text" },
"tag": { "type": "keyword" }
}
}
}Searching for "quick brown" in title would find "The quick brown fox". Searching for "quick brown" in tag would only match if the tag was *exactly* "quick brown".
Numeric Field Types
Elasticsearch provides various numeric types to store whole numbers and decimals efficiently. Choosing the right type saves space and optimizes query performance.
- Whole Numbers:
long,integer,short,byte. Useintegerfor age,longfor large IDs. - Decimal Numbers:
double,float,half_float,scaled_float. Usefloatordoublefor prices or measurements.
For example, "age": { "type": "integer" }.
Date Field Type
The date field type is used for storing dates and times. Elasticsearch supports many standard date formats by default, like ISO 8601.
You can also define a custom format if your dates are in a specific pattern:
"publish_date": {
"type": "date",
"format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd"
}Dates are internally stored as milliseconds since the epoch, which allows for efficient range queries and sorting.
Boolean Field Type
The boolean field type is simple and efficient for storing true or false values. It's perfect for binary flags or status indicators.
For example, to indicate if a product is currently available:
"is_available": {
"type": "boolean"
}Elasticsearch accepts various representations for true/false, such as "true", "false", "T", "F", "on", "off", "yes", "no", "1", "0".
A Full Custom Mapping Example
Let's combine what we've learned to create a comprehensive mapping for a typical e-commerce product index:
PUT /products_catalog
{
"mappings": {
"properties": {
"product_id": { "type": "keyword" },
"name": { "type": "text" },
"description": { "type": "text" },
"price": { "type": "float" },
"stock_quantity": { "type": "integer" },
"category": { "type": "keyword" },
"release_date": { "type": "date", "format": "yyyy-MM-dd" },
"is_featured": { "type": "boolean" }
}
}
}Mapping Quiz
A field named "order_id" stores unique transaction identifiers like "TXN-2023-007". You need to be able to filter and sort orders by this ID precisely. Which field type is most appropriate?
Recap: Custom Mappings
Great job! You've taken a deep dive into explicitly defining field mappings in Elasticsearch.
We covered:
- Why custom mappings are essential for precise control.
- The basic structure for defining index mappings.
- Key field types:
text,keyword,numeric,date, andboolean. - How to choose the right type for your specific data needs.
Customizing mappings is a fundamental skill for building efficient and powerful search applications!
常见问题解答
「自定义字段映射」课时是免费的吗?
是的 — 「自定义字段映射」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 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 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「自定义字段映射」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Elasticsearch & Full Text Search Systems 课中编写并运行代码吗?
能。每节 Elasticsearch & Full Text Search Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 自定义字段映射
- 动态映射与显式映射
- 索引模板与别名
- 嵌套字段与对象字段类型