Elasticsearch · Lesson 11 of 120
Indexing a Document
PUT with an ID.
⏱ 2 min read▶ Video at 10:04🎓 Free
Storing a document in Elasticsearch is called indexing it. You send the JSON, and choose an ID, like the product number from your database.
- Indexing = storing
- Send JSON
- Choose an ID
The path is the index, then_doc, then the ID. The body is the product. Run it. The result is created, and this is version one.
Dev Tools Consoletext
PUT products/_doc/1
{
"name": "Basecamp 2-Person Tent",
"brand": "TrailCo",
"price": 237.99,
"in_stock": true
}
Output
{
"_index": "products",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {"total": 1, "successful": 1, "failed": 0},
"_seq_no": 0,
"_primary_term": 1
}Also uses _setup.http
PUT products
{ "settings": { "number_of_replicas": 0 } }
What if we PUT the same ID again, with a new price? Run it. The result is updated, and the version is now two. PUT replaces the whole document.
Dev Tools Consoletext
PUT products/_doc/1?filter_path=result,_version
{
"name": "Basecamp 2-Person Tent",
"brand": "TrailCo",
"price": 219.99,
"in_stock": true
}
Output
{"_version": 2, "result": "updated"}Also uses _setup.http
PUT products
{ "settings": { "number_of_replicas": 0 } }
PUT products/_doc/1
{ "name": "Basecamp 2-Person Tent", "brand": "TrailCo", "price": 237.99, "in_stock": true }
PUT index, underscore doc, ID stores a document. PUT the same ID again, and it replaces the document and bumps the version. Next, getting it back.
Key takeaways- PUT _doc/1
- created
- updated, version 2