使用文档执行 CRUD 操作
使用 Elasticsearch 的 REST API 掌握文档的创建、读取、更新和删除操作,包括部分更新。
使用文档执行 CRUD 操作 是 CoddyKit 上的免费 Elasticsearch & Full Text Search Systems 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elasticsearch & Full Text Search Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What are CRUD Operations?
In the world of data, CRUD is a fundamental concept. It stands for Create, Read, Update, and Delete.
These are the four basic operations you perform on data in any system, including Elasticsearch. Mastering them is key to managing your search documents effectively.
Indexing New Documents
Adding a new document to Elasticsearch is called indexing. You can index documents:
- By letting Elasticsearch generate a unique ID (using
POST). - By providing your own custom ID (using
PUT).
The document content is sent as a JSON body.
Example: Indexing a Book
Let's index a book document. We'll use PUT and specify books/_doc/101 to give it a custom ID of 101.
PUT /books/_doc/101
{
"title": "The Hitchhiker's Guide to the Galaxy",
"author": "Douglas Adams",
"year": 1979
}Retrieving Documents
To fetch a document, you use the Get API. You just need to know its unique ID (_id) and the index it's in.
Elasticsearch will return the document's source (_source) along with metadata like its _index, _id, and _version.
Example: Getting Our Book
Now, let's retrieve the book we just indexed using its ID.
GET /books/_doc/101Full Document Replacement
One way to update a document is to completely replace it. You use the PUT method with the same document ID.
Be careful! Any fields missing from your new PUT request will be deleted from the original document.
Example: Adding a Genre
Let's add a genre field to our book. Notice how we provide the full document again, including existing fields.
PUT /books/_doc/101
{
"title": "The Hitchhiker's Guide to the Galaxy",
"author": "Douglas Adams",
"year": 1979,
"genre": "Science Fiction"
}Efficient Partial Updates
For small changes, fully replacing a document is inefficient. Elasticsearch provides the _update API for partial updates.
This allows you to send only the fields you want to change, making the operation faster and using less bandwidth. You use the POST method.
Example: Changing a Field
Let's update just the year field of our book using the _update API and the "doc" parameter. This merges the changes.
POST /books/_update/101
{
"doc": {
"year": 1980
}
}Removing Documents
To permanently remove a document from your index, you use the Delete API. Simply specify the index and the document's ID.
Once deleted, the document is gone and cannot be retrieved.
DELETE /books/_doc/101Quick Check
You have a document with fields name, age, and city. You want to change only the city field to a new value. Which is the most efficient and recommended way to do this in Elasticsearch?
Recap: CRUD in Elasticsearch
Great job! You've learned the essential CRUD operations for managing documents in Elasticsearch:
- Create (Index): Add new documents with
PUT(custom ID) orPOST(auto ID). - Read (Get): Retrieve documents by ID using
GET. - Update: Replace documents entirely with
PUTor perform efficient partial updates withPOST /_update. - Delete: Remove documents by ID using
DELETE.
These operations form the backbone of interacting with your Elasticsearch data.
常见问题解答
「使用文档执行 CRUD 操作」课时是免费的吗?
是的 — 「使用文档执行 CRUD 操作」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elasticsearch & Full Text Search Systems 课程的其余内容,请升级到 CoddyKit PRO。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
「使用文档执行 CRUD 操作」这节课中我会学到什么?
使用 Elasticsearch 的 REST API 掌握文档的创建、读取、更新和删除操作,包括部分更新。 你通过在浏览器中直接运行的动手代码来练习 Elasticsearch & Full Text Search Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Elasticsearch & Full Text Search Systems 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Elasticsearch & Full Text Search Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用文档执行 CRUD 操作」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Elasticsearch & Full Text Search Systems 课中编写并运行代码吗?
能。每节 Elasticsearch & Full Text Search Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 将文档索引到 Elasticsearch
- 使用文档执行 CRUD 操作
- 映射与数据类型基础
- 批量建立索引与批量接口