ElasticSearch update syntax
Overwrite Update
You can use the _update
API to update a single document. This method is suitable for cases where you need to overwrite the entire document.
POST /my_index/_update/1
{
"doc": {
"title": "Updated Title",
"content": "Updated content."
}
}
Partial Update
Sometimes, you only need to update certain fields of a document, rather than the entire document. Elasticsearch supports partial updates, updating only the specified fields.
Example:
POST /my_index/_update/1
{
"doc": {
"content": "Updated content."
}
}
In this example, only the content
field is updated, while other fields in the document remain unchanged.
Script Update
You can also use a script to update documents. This method is particularly useful for performing calculations or conditional updates on existing fields.
Example:
POST /my_index/_update/1
{
"script": {
"source": "ctx._source.view_count += params.count",
"lang": "painless",
"params": {
"count": 1
}
}
}
Bulk Update
When you need to update multiple documents at once, you can use the _bulk
API for bulk updates. This method can significantly improve performance.
Example:
POST /_bulk
{ "update": { "_id": "1", "_index": "my_index" } }
{ "doc": { "content": "Bulk updated content." } }
{ "update": { "_id": "2", "_index": "my_index" } }
{ "doc": { "content": "Another bulk updated content." } }
Updating Nested Fields
Updating nested fields in Elasticsearch can be complex because nested objects are essentially separate documents. Updating nested fields typically requires a script update.
Assume we have an index containing order information, with each order containing a list of nested products. Now, suppose we want to update the price of the product "Laptop" in the order with order_id
1001. We can achieve this using the following script update:
POST /orders/_update/1
{
"script": {
"source": """
for (int i = 0; i < ctx._source.products.size(); i++) {
if (ctx._source.products[i].name == params.product_name) {
ctx._source.products[i].price = params.new_price;
}
}
""",
"lang": "painless",
"params": {
"product_name": "Laptop",
"new_price": 1300
}
}
}
Considerations
- Concurrency Control: When multiple clients are updating the same document concurrently, conflicts may occur. Use
_version
or_seq_no
and_primary_term
to achieve concurrency control. - Performance Optimization: Bulk updates can improve performance, but ensure you choose a reasonable bulk size to avoid memory overflow.
- Script Security: Ensure script security to prevent the execution of malicious code when using script updates.