Elasticsearch · Lesson 12 of 120
Getting a Document
GET by ID.
⏱ 2 min read▶ Video at 10:52🎓 Free
If you know a document's ID, you don't need to search. You can fetch it straight away, and it's always up to date, even before a refresh.
- Know the ID?
- Fetch it directly
- Always current
GET, the index, underscore doc, and the ID. Run it. Found is true, and_source holds the JSON we stored.
Dev Tools Consoletext
GET products/_doc/1
Output
{
"_index": "products",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"name": "Basecamp 2-Person Tent",
"brand": "TrailCo",
"price": 237.99,
"in_stock": true
}
}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 }
Asking for ID ninety nine gives found false, and a four oh four status. And_source, instead of_doc, returns only the document itself. Run it.
Dev Tools Consoletext
GET products/_doc/99
GET products/_source/1
Output
# GET products/_doc/99 404 Not Found
{"_index": "products", "_id": "99", "found": false}
# GET products/_source/1 200 OK
{
"name": "Basecamp 2-Person Tent",
"brand": "TrailCo",
"price": 237.99,
"in_stock": true
}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 }
GET_doc fetches by ID, found tells you if it exists, and_source returns just the JSON. Next, letting Elasticsearch choose the ID.
Key takeaways