AI Hints
Home›Elasticsearch Tutorial›Create, Update, Delete
Elasticsearch · Lesson 13 of 120

Auto IDs with POST

Let Elasticsearch choose.

⏱ 2 min read▶ Video at 11:39🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Some data has no natural ID, like log lines or page views. For those, let Elasticsearch create a unique ID for you.

  • No natural ID
  • Logs, events
  • Auto ID

POST without an ID

Use POST instead of PUT, and leave out the ID. Run it. Elasticsearch made up a random ID for the new document. Each POST creates a new document, even with the same body.

Dev Tools Consoletext
POST products/_doc?filter_path=_id,result
{
  "name": "Pocket Camping Stove",
  "price": 49.99
}
Output
{"_id": "Ndwm26EBS3fm6LIGQYay", "result": "created"}
Also uses _setup.http
PUT products
{ "settings": { "number_of_replicas": 0 } }

Create only

Sometimes you want to add a document, but never overwrite one. The create endpoint does that. The first request works. Run it. The second fails with a conflict, because ID one already exists.

Dev Tools Consoletext
PUT products/_create/1?filter_path=result
{ "name": "Basecamp 2-Person Tent" }

PUT products/_create/1
{ "name": "Something else" }
Output
# PUT products/_create/1 201 Created
{"result": "created"}

# PUT products/_create/1 409 Conflict
{
  "error": {
    "type": "version_conflict_engine_exception",
    "reason": "[1]: version conflict, document already exists (current version [1])"
  },
  "status": 409
}
Also uses _setup.http
PUT products
{ "settings": { "number_of_replicas": 0 } }

Summary

POST_doc creates a document with an automatic ID. Underscore create adds a document only if the ID is new. Next, updating documents.

Key takeaways
  • POST _doc
  • Auto ID
  • _create
Next up: Updating Documents — change one field