Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
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.

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

The 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.

Changing book 1's priceStep 1 of 4
  1. Tempting

    PUT /books/_doc/1 with { "price": 10.0 }.

Drills

Answer fast — trust the rule.

quick drill

Book 1 is {"title": "The Quiet Garden", "price": 12.5}. You send PUT /books/_doc/1 with {"price": 10.0}. What is stored now?

quick drill

Which request fails if a document with ID 4 already exists?

quick drill

What does DELETE /books remove?

quick drill

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 _doc replaces; _update with doc merges; _create refuses to overwrite.
  • DELETE /books wipes the index and all its documents.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainReplace or merge?

Book 1 is stored as {"title": "The Quiet Garden", "price": 12.5}. Explain what is stored after PUT /books/_doc/1 with {"price": 10.0}, what you should send instead to change only the price, and how _version changes.