Indexing a document means saving data into Elasticsearch so you can search it later. It helps organize and store information quickly.
Indexing a document (POST/PUT) in Elasticsearch
POST /index_name/_doc
{
"field1": "value1",
"field2": "value2"
}
PUT /index_name/_doc/document_id
{
"field1": "new_value1",
"field2": "new_value2"
}Use POST without an ID to let Elasticsearch create a unique ID automatically.
Use PUT with a specific ID to create or replace a document with that ID.
POST /products/_doc
{
"name": "Coffee Mug",
"price": 12.99
}PUT /products/_doc/123 { "name": "Tea Cup", "price": 9.99 }
POST /logs/_doc
{
"event": "user_login",
"user": "alice"
}This example adds a book document to the 'library' index with details about the title, author, and year.
POST /library/_doc
{
"title": "The Little Prince",
"author": "Antoine de Saint-Exupéry",
"year": 1943
}Elasticsearch automatically creates the index if it does not exist when you index a document.
Using PUT with an existing ID replaces the whole document.
Always check the response to confirm if the document was created or updated.
Indexing stores data in Elasticsearch for searching.
Use POST to add with auto ID, PUT to add or replace with a specific ID.
Check the response to know if the operation succeeded.
Practice
POST and PUT when indexing a document in Elasticsearch?Solution
Step 1: Understand
POSTbehaviorPOSTindexes a document and Elasticsearch generates a unique ID automatically.Step 2: Understand
PUTbehaviorPUTindexes a document with the ID you specify, replacing any existing document with that ID.Final Answer:
POSTadds a document with an auto-generated ID,PUTadds or replaces a document with a specified ID. -> Option AQuick Check:
Method difference = auto ID vs specified ID [OK]
- Thinking POST deletes documents
- Confusing PUT with search operation
- Assuming PUT never requires an ID
PUT in Elasticsearch?Solution
Step 1: Identify correct HTTP method and URL
To specify an ID, usePUTwith the URL including the ID:/index/_doc/123.Step 2: Confirm request body format
The body contains the document fields as JSON, e.g., {"field": "value"}.Final Answer:
PUT /index/_doc/123 {"field": "value"} -> Option DQuick Check:
PUT + ID in URL = correct syntax [OK]
- Using POST with an ID in URL
- Omitting the ID when using PUT
- Confusing URL structure
POST /products/_doc
{
"name": "Coffee Mug",
"price": 12.99
}
What will Elasticsearch do after this request?Solution
Step 1: Analyze the HTTP method and URL
The request usesPOSTon/products/_doc, which means index 'products' and type '_doc'.Step 2: Understand ID assignment
Since no ID is specified, Elasticsearch generates a unique ID automatically and indexes the document.Final Answer:
Index the document with an auto-generated ID under the 'products' index. -> Option AQuick Check:
POST without ID = auto-generated ID [OK]
- Thinking missing ID causes error
- Assuming document replaces existing one
- Confusing index and type names
PUT /myindex/_doc without specifying an ID. What is the problem with this request?Solution
Step 1: Check the HTTP method and URL requirements
PUTrequires an explicit document ID in the URL to index or replace a document.Step 2: Identify the error cause
Since the ID is missing, Elasticsearch will reject the request with an error due to invalid URL syntax.Final Answer:
It will cause a syntax error because ID is missing in the URL. -> Option CQuick Check:
PUT needs ID in URL [OK]
- Assuming PUT auto-generates ID
- Thinking it deletes index
- Believing it updates all documents
Solution
Step 1: Understand update or create behavior
PUTwith a specified ID will create the document if missing or replace it if it exists.Step 2: Compare options
POSTwithout ID creates new documents with auto IDs, so it won't update by ID. Deleting then posting is inefficient.Final Answer:
UsePUT /users/_doc/789with the document body. -> Option BQuick Check:
PUT with ID updates or creates [OK]
- Specifying ID in the document body
- Omitting ID when update needed
- Deleting before posting wastes resources
Ready to prove it?
Pass the test and this pattern moves unknown
