| endpoint | index |
|---|---|
| lang | python |
| es_version | 9.3 |
| client | elasticsearch==9.3.0 |
Use client.index() to create or replace a document in an index.
response = client.index(
index="products",
id="prod-1",
document={
"name": "Espresso Machine Pro",
"brand": "BrewMaster",
"price": 899.99,
"category": "appliances",
"in_stock": True,
"rating": 4.7,
},
)
print(f"{response['result']} document {response['_id']}")If a document with the given id already exists, it is replaced
entirely. Omit id to let Elasticsearch generate one automatically.
Use if_seq_no and if_primary_term to prevent overwriting changes
made by another process:
doc = client.get(index="products", id="prod-1")
response = client.index(
index="products",
id="prod-1",
document={"name": "Espresso Machine Pro", "price": 849.99},
if_seq_no=doc["_seq_no"],
if_primary_term=doc["_primary_term"],
)If the document was modified since the get, this raises a
ConflictError (HTTP 409).
response = client.index(
index="products",
id="prod-1",
document={"name": "Espresso Machine Pro", "price": 899.99},
routing="appliances",
refresh="wait_for",
pipeline="enrich-products",
)