Introduction to Amazon Elasticsearch

   Go back to the Task List

  « 4. Search & Query Data    6. Delete the Data »

5. Update the Data

In this step, you update the records in the index. You will use doc and script methods to update the records.

  1. Post the following URL with the request body to update duration field equal to 7 for the record with id = 1. Replace {ES_ENDPOINT_URL} with Elasticsearch Endpoint.

    URL: {ES_ENDPOINT_URL}/organization/employee/1/_update

    {
        "doc" : {
            "duration" : 7
        }
    }
    

    `

    Amazon Elasticsearch

  2. Post the following URL with the request body. If there is a record with id = 6, the record will be updated otherwise the record will be inserted.

    URL: {ES_ENDPOINT_URL}/organization/employee/6/_update

    {
      "doc": {
        "name": "Victor Bross",
        "department": "Sales",
        "age": 47,
        "duration": 10
      },
      "doc_as_upsert": true
    }
    

    `

    Amazon Elasticsearch

  3. You can also pass parameters when updating the record. Post the following URL with the request body. It will update the record with id = 1 and increase the age by 4 which is passed as a parameter. You can also see the version number of the record increasing with each update.

    URL: {ES_ENDPOINT_URL}/organization/_update/1

    {
      "script" : {
        "source": "ctx._source.age += params.count",
        "lang": "painless",
        "params" : {
          "count" : 4
        }
      }
    }
    

    `

    Amazon Elasticsearch

  4. You can also add a new field to the record as part of the update. Post the following URL with the request body. It will update the record with id = 1 with a new field salary.

    URL: {ES_ENDPOINT_URL}/organization/_update/1

    {
      "script" : "ctx._source.salary = 12000"
    }
    

    `

    Amazon Elasticsearch

  5. Similarly, you can also remove a field from the record when updating the record. Post the following URL with the request body. It will update the record with id = 1 by removing the field salary.

    URL: {ES_ENDPOINT_URL}/organization/_update/1

    {
      "script" : "ctx._source.remove('salary')"
    }
    

    `

    Amazon Elasticsearch

  6. You learnt updating the records. You learn deleting the records in the next step.