liveBook · Manning

_doc endpoint

As explained in Elasticsearch in Action, Second Edition

The _doc endpoint is an Elasticsearch document endpoint used to address, create, or replace a document within an index.

Use the `_doc` endpoint to write one JSON document at a known location in an Elasticsearch index. With `PUT`, the URL includes the index, `_doc`, and a client-chosen document ID; Elasticsearch stores the request body under that ID. section 5.1.1

Index a movie with an ID

`PUT movies/_doc/1` writes a movie document to the `movies` index with ID `1`. The JSON body contains the document data, such as its title and synopsis. section 5.1.1

Listing 5.1 Indexing a new document into the movies index
PUT movies/_doc/1           #1
{                           #2
  "title":"The Godfather", 
  "synopsis":"The aging patriarch of an organized crime dynasty transfers 
   control of his clandestine empire to his reluctant son"
}

The request path separates the index (`movies`), document endpoint (`_doc`), and document identifier (`1`) from the JSON payload. section 5.1.1

When no ID is supplied, send `POST <index>/_doc`; Elasticsearch assigns a random UUID. Use `PUT <index>/_doc/<id>` when the client supplies the ID. section 5.1.1

Sending another `PUT` request to the same `_doc` ID replaces the existing document with the new request body. Choose `_create` instead when existing content must not be replaced. section 5.4.4 section 5.1.1

Listing 5.30 Replacing the content of a document
PUT movies/_doc/1
{
  "title":"Avatar"
}

Reusing ID `1` with a new body replaces the earlier movie data with the `Avatar` document. section 5.4.4

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains request paths, HTTP verb choice, generated IDs, and replacement behavior without detailing document internals.
Examples
Uses movie indexing, a movie review without an ID, and replacement of The Godfather with Avatar.
What it assumes
Introduces document indexing through concrete REST paths and JSON request bodies without requiring prior endpoint conventions.
What you can do after
Apply it via Elasticsearch REST requests to create indexed documents or replace a document at a known ID.
How it connects
Connects `_doc` to HTTP PUT, HTTP POST, document IDs, `_create`, and upsert operations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.