Document IDs help find and update data quickly in Elasticsearch. You can let Elasticsearch create them automatically or set your own IDs.
Document ID strategies (auto vs manual) in Elasticsearch
POST /index_name/_doc
{
"field": "value"
}
POST /index_name/_doc/document_id
{
"field": "value"
}Without specifying an ID, Elasticsearch creates one automatically.
To set your own ID, add it after the index and _doc in the URL.
POST /products/_doc
{
"name": "Coffee Mug",
"price": 12.99
}POST /products/_doc/12345 { "name": "Tea Cup", "price": 9.99 }
GET /products/_doc/12345This example adds two books to the 'library' index. The first book gets an automatic ID. The second book uses a manual ID 'book-001'. Then it retrieves the second book by its manual ID.
POST /library/_doc
{
"title": "Elasticsearch Basics",
"author": "Jane Doe"
}
POST /library/_doc/book-001
{
"title": "Advanced Elasticsearch",
"author": "John Smith"
}
GET /library/_doc/book-001Manual IDs let you update or delete documents easily by ID.
Automatic IDs are good when you don't have a unique key and want Elasticsearch to handle uniqueness.
Using manual IDs incorrectly can cause overwriting documents if IDs repeat.
Document IDs identify each document uniquely in Elasticsearch.
You can let Elasticsearch create IDs automatically or set your own manual IDs.
Manual IDs help with updates and avoiding duplicates but require careful management.