การทำดัชนีเอกสารใน Elasticsearch
ทำความเข้าใจวิธีทำดัชนีเอกสารหนึ่งรายการและหลายรายการในดัชนี Elasticsearch รวมถึงการสร้าง ID อัตโนมัติและ ID แบบกำหนดเอง
การทำดัชนีเอกสารใน Elasticsearch เป็นบทเรียน Elasticsearch & Full Text Search Systems ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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!
เรียนรู้ Elasticsearch & Full Text Search Systems ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การทำดัชนีเอกสารใน Elasticsearch” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทำดัชนีเอกสารใน Elasticsearch” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elasticsearch & Full Text Search Systems ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elasticsearch & Full Text Search Systems มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทำดัชนีเอกสารใน Elasticsearch”
ทำความเข้าใจวิธีทำดัชนีเอกสารหนึ่งรายการและหลายรายการในดัชนี Elasticsearch รวมถึงการสร้าง ID อัตโนมัติและ ID แบบกำหนดเอง คุณปฏิบัติ Elasticsearch & Full Text Search Systems ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elasticsearch & Full Text Search Systems หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elasticsearch & Full Text Search Systems บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การทำดัชนีเอกสารใน Elasticsearch” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Elasticsearch & Full Text Search Systems นี้ได้ไหม
ได้ บทเรียน Elasticsearch & Full Text Search Systems ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การทำดัชนีเอกสารใน Elasticsearch
- การดำเนินการ CRUD กับเอกสาร
- การแมปและประเภทข้อมูลพื้นฐาน
- การสร้างดัชนีจำนวนมากและ Bulk API