Skip to the workspace
Pebellslearn by building
Documents and Leaf Queries  ›  Lesson

Create, Read, Update and Delete — Indices and Documents

Index Document 12 minIndex-level requests act on the index path, document-level requests act on the index, _doc and an ID, and PUT replaces while _update merges.

elasticsearch / Create, Read, Update and Delete — Indices and Documents

Explore each scene at your own pace.

01 · The rule

The rule

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 /books

PUT 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/1

The 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/1
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.