In this section, we will cover how to update and delete data in Elasticsearch. These operations are crucial for maintaining the accuracy and relevance of your data. We will explore the following topics:

  1. Updating Documents
  2. Deleting Documents
  3. Bulk Operations
  4. Practical Exercises

  1. Updating Documents

Updating documents in Elasticsearch can be done in several ways. The most common methods are:

  • Partial Update: Update specific fields of a document.
  • Scripted Update: Use a script to update a document.

Partial Update

A partial update allows you to update specific fields of a document without sending the entire document. This is done using the _update endpoint.

Example:

POST /my_index/_update/1
{
  "doc": {
    "field1": "new_value"
  }
}

In this example, the document with ID 1 in the index my_index will have its field1 updated to new_value.

Scripted Update

A scripted update allows you to use a script to update a document. This is useful for more complex updates.

Example:

POST /my_index/_update/1
{
  "script": {
    "source": "ctx._source.field1 += params.increment",
    "params": {
      "increment": 5
    }
  }
}

In this example, the script increments the value of field1 by 5 for the document with ID 1 in the index my_index.

  1. Deleting Documents

Deleting documents in Elasticsearch can be done using the _delete endpoint. You can delete a single document or multiple documents using a query.

Deleting a Single Document

To delete a single document, you need to specify the index and the document ID.

Example:

DELETE /my_index/_doc/1

In this example, the document with ID 1 in the index my_index will be deleted.

Deleting Documents by Query

You can delete multiple documents that match a specific query using the _delete_by_query endpoint.

Example:

POST /my_index/_delete_by_query
{
  "query": {
    "match": {
      "field1": "value_to_delete"
    }
  }
}

In this example, all documents in the index my_index where field1 matches value_to_delete will be deleted.

  1. Bulk Operations

Elasticsearch provides a bulk API to perform multiple update and delete operations in a single request. This is useful for improving performance when dealing with large datasets.

Bulk Update

Example:

POST /_bulk
{ "update": { "_id": "1", "_index": "my_index" } }
{ "doc": { "field1": "new_value" } }
{ "update": { "_id": "2", "_index": "my_index" } }
{ "doc": { "field1": "another_value" } }

In this example, two documents in the index my_index are updated in a single bulk request.

Bulk Delete

Example:

POST /_bulk
{ "delete": { "_id": "1", "_index": "my_index" } }
{ "delete": { "_id": "2", "_index": "my_index" } }

In this example, two documents in the index my_index are deleted in a single bulk request.

  1. Practical Exercises

Exercise 1: Partial Update

  1. Create an index test_index and add a document with ID 1 and the following content:
    {
      "name": "John Doe",
      "age": 30,
      "city": "New York"
    }
    
  2. Update the age field to 31 using a partial update.

Solution:

POST /test_index/_update/1
{
  "doc": {
    "age": 31
  }
}

Exercise 2: Scripted Update

  1. Create an index test_index and add a document with ID 2 and the following content:
    {
      "name": "Jane Doe",
      "age": 25,
      "city": "Los Angeles"
    }
    
  2. Increment the age field by 1 using a scripted update.

Solution:

POST /test_index/_update/2
{
  "script": {
    "source": "ctx._source.age += params.increment",
    "params": {
      "increment": 1
    }
  }
}

Exercise 3: Delete by Query

  1. Create an index test_index and add multiple documents with the city field set to New York.
  2. Delete all documents where the city field is New York.

Solution:

POST /test_index/_delete_by_query
{
  "query": {
    "match": {
      "city": "New York"
    }
  }
}

Conclusion

In this section, we covered how to update and delete data in Elasticsearch. We explored partial updates, scripted updates, and bulk operations. We also provided practical exercises to reinforce the learned concepts. Understanding these operations is essential for maintaining and managing your Elasticsearch indices effectively. In the next section, we will dive into the Elasticsearch Query DSL to perform more advanced searches.

© Copyright 2024. All rights reserved