Operacje CRUD na dokumentach
Opanuj operacje tworzenia, odczytu, aktualizowania i usuwania dokumentów za pomocą interfejsu REST API Elasticsearch, w tym aktualizacje częściowe.
Operacje CRUD na dokumentach to bezpłatna lekcja Elasticsearch & Full Text Search Systems na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Elasticsearch & Full Text Search Systems, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Elasticsearch & Full Text Search Systems zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „Operacje CRUD na dokumentach” jest bezpłatna?
Tak — pełny tekst „Operacje CRUD na dokumentach” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Elasticsearch & Full Text Search Systems, przejdź na CoddyKit PRO. Kurs Elasticsearch & Full Text Search Systems zawiera 4 lekcji w sumie.
Co nauczysz się w „Operacje CRUD na dokumentach”?
Opanuj operacje tworzenia, odczytu, aktualizowania i usuwania dokumentów za pomocą interfejsu REST API Elasticsearch, w tym aktualizacje częściowe. Ćwiczysz Elasticsearch & Full Text Search Systems z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Elasticsearch & Full Text Search Systems?
Nie wymagamy żadnego doświadczenia. Elasticsearch & Full Text Search Systems w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Operacje CRUD na dokumentach”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Elasticsearch & Full Text Search Systems?
Tak. Każda lekcja Elasticsearch & Full Text Search Systems zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Indeksowanie dokumentów w Elasticsearch
- Operacje CRUD na dokumentach
- Podstawy mapowania i typów danych
- Indeksowanie zbiorcze i Bulk API