Create, Read, Update and Delete — Indices and Documents
Every search engine feature rests on a handful of everyday requests: make an index, put a book in, read it back, change its price, take it out, and throw the index away. Six requests, one pattern.
The rule
Index verbs act on
/books; document verbs act on/books/_doc/<id>.
Create and delete an index:
PUT /books
{ "mappings": { "properties": { "title": { "type": "text" }, "price": { "type": "float" } } } }
DELETE /booksPUT on an existing index fails with resource_already_exists_exception. DELETE /books removes the index and every document in it — there is no undo short of a snapshot.
Index (store) a document:
PUT /books/_doc/1
{ "title": "The Quiet Garden", "price": 12.5 }
POST /books/_doc
{ "title": "Gardening for Busy People", "price": 18.0 }PUT with an ID creates the document, or replaces it completely if the ID exists. POST without an ID lets Elasticsearch generate one. To create only if the ID is free, use PUT /books/_create/1 — it returns 409 Conflict if book 1 already exists.
Get a document:
GET /books/_doc/1The reply holds "found": true, the _version, and the original JSON in _source. A missing ID gives 404 with "found": false.
Update part of a document:
POST /books/_update/1
{ "doc": { "price": 10.0 } }Only price changes; title is kept. Behind the scenes documents are immutable: Elasticsearch reads the old version, merges your change, and writes a whole new version, bumping _version.
Delete a document:
DELETE /books/_doc/1The shortcut
Before sending, name the target (index or one document?) and the effect (create, replace, merge, remove?). The path gives the first, the verb plus endpoint gives the second.
Tempting
PUT /books/_doc/1with{ "price": 10.0 }.
Drills
Answer fast — trust the rule.
Book 1 is {"title": "The Quiet Garden", "price": 12.5}. You send PUT /books/_doc/1 with {"price": 10.0}. What is stored now?
Which request fails if a document with ID 4 already exists?
What does DELETE /books remove?
You want Elasticsearch to choose a new book's ID. Which request?
Remember
- Index verbs act on
/books; document verbs act on/books/_doc/<id>. - PUT on
_docreplaces;_updatewithdocmerges;_createrefuses to overwrite. DELETE /bookswipes the index and all its documents.
