Book Image

ElasticSearch Cookbook

By : Alberto Paro
Book Image

ElasticSearch Cookbook

By: Alberto Paro

Overview of this book

Table of Contents (20 chapters)
ElasticSearch Cookbook Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Updating a document using scripts


ElasticSearch allows you to update a document in its place.

Updating a document via scripting reduces the network traffic (otherwise, you would need to fetch the document, change the field, and send it back) and improves performance when you need to process huge amounts of documents.

Getting ready

You will need a working ElasticSearch cluster and an index populated with the script used in Chapter 6, Aggregations, which is available at https://github.com/aparo/elasticsearch-cookbook-second-edition.

How to do it...

In order to update a document using scripting, perform the following steps:

  1. Write an update action that adds a tag value to a list of tags available in the source of a document. This is how the code should look:

    curl -XPOST 'http://127.0.0.1:9200/test-index/test-type/9/_update?&pretty=true' -d '{
      "script" : "ctx._source.tag += tag",
      "params" : {
        "tag" : "cool"
      }
    }'
    
  2. If everything works correctly, this is how the result returned by ElasticSearch...