将文档索引到 Elasticsearch
了解如何将单个和多个文档索引到 Elasticsearch 索引中,包括自动生成 ID 和自定义 ID。
将文档索引到 Elasticsearch 是 CoddyKit 上的免费 Elasticsearch & Full Text Search Systems 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elasticsearch & Full Text Search Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is Indexing?
Welcome to indexing! In Elasticsearch, indexing is the process of storing data into an index to make it searchable.
Think of it like adding a new book to a library's catalog. You provide the book's details, and the library stores them in a way that makes the book easy to find later.
Documents & Indices Refresher
Before we dive in, let's quickly recap two core concepts:
- Document: A basic unit of information in Elasticsearch, similar to a row in a traditional database. It's usually a JSON object.
- Index: A collection of documents that have similar characteristics. It's like a database in a relational world.
When you index, you add a document to an index.
The Index API
You interact with Elasticsearch using its REST API. To index a document, you'll typically use HTTP POST or PUT requests.
POST /<index>/_doc: Used to index a document, often letting Elasticsearch generate an ID.PUT /<index>/_doc/<id>: Used to index a document with a specific, user-provided ID.
Let's see them in action!
Auto-Generated IDs
The simplest way to index is to let Elasticsearch generate a unique ID for your document. You use the POST method to the _doc endpoint without specifying an ID.
Here's an example using curl to index a document into an index named products:
curl -X POST "localhost:9200/products/_doc?pretty" \
-H 'Content-Type: application/json' \
-d'{"name": "Laptop", "price": 1200}'Understanding Auto IDs
After the previous POST request, Elasticsearch would return a response including a unique _id for your document, like "_id": "AbCdEfGhIjKlMnOpQrSt".
When should you use auto-generated IDs?
- When you don't have a natural unique identifier for your data.
- For logs or temporary data where a unique ID isn't critical for external reference.
- When you want to guarantee a new document is always created.
Indexing with Custom IDs
Often, your data already has a unique identifier from another system (e.g., a database primary key). In such cases, you can provide your own ID using the PUT method.
The ID is specified directly in the URL path: /<index>/_doc/<your_id>.
curl -X PUT "localhost:9200/products/_doc/prod_101?pretty" \
-H 'Content-Type: application/json' \
-d'{"name": "Smartphone", "price": 800}'Why Use Custom IDs?
Using custom IDs offers several advantages:
- Integration: Easily map Elasticsearch documents to records in an external database.
- Predictability: You know the document's ID beforehand.
- Updates: It makes updating specific documents straightforward, as you always refer to them by their known ID.
Idempotency with PUT
A key concept when using PUT with a custom ID is idempotency. This means that performing the same operation multiple times will produce the same result as performing it once.
- If a document with the specified ID already exists,
PUTwill update it. - If it doesn't exist,
PUTwill create it.
This is different from POST, which always creates a *new* document with a new ID.
Indexing Many Documents
While indexing documents one-by-one is fine for small numbers, it can be inefficient for large datasets due to network overhead.
Elasticsearch provides a powerful _bulk API that allows you to perform multiple index, update, or delete operations in a single request. This dramatically improves indexing performance.
We'll explore the _bulk API in more detail in a future lesson!
Indexing Method Check
Imagine you have a new set of sensor readings. Each reading is unique, and you don't have a predefined ID for them, but you want to store them in Elasticsearch to be searchable.
Recap: Indexing Essentials
Great job! In this lesson, you learned the fundamentals of indexing documents into Elasticsearch:
- What indexing means and its role in making data searchable.
- The difference between documents and indices.
- How to use
POST /<index>/_docto index documents with auto-generated IDs. - How to use
PUT /<index>/_doc/<id>to index documents with custom IDs. - The concept of idempotency when using
PUT. - A brief introduction to the efficiency of bulk indexing.
Next, we'll explore more operations on these documents!
用 AI 导师学习 Elasticsearch & Full Text Search Systems — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「将文档索引到 Elasticsearch」课时是免费的吗?
是的 — 「将文档索引到 Elasticsearch」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elasticsearch & Full Text Search Systems 课程的其余内容,请升级到 CoddyKit PRO。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
「将文档索引到 Elasticsearch」这节课中我会学到什么?
了解如何将单个和多个文档索引到 Elasticsearch 索引中,包括自动生成 ID 和自定义 ID。 你通过在浏览器中直接运行的动手代码来练习 Elasticsearch & Full Text Search Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Elasticsearch & Full Text Search Systems 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Elasticsearch & Full Text Search Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「将文档索引到 Elasticsearch」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Elasticsearch & Full Text Search Systems 课中编写并运行代码吗?
能。每节 Elasticsearch & Full Text Search Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 将文档索引到 Elasticsearch
- 使用文档执行 CRUD 操作
- 映射与数据类型基础
- 批量建立索引与批量接口