Replacing Documents

Published on January 17, 2016 by

In this part of the series, I will show you how you can easily replace a document within an index.

In fact, the way to do this is identical to how you add a document to an index. Therefore, what you have to do, is to issue a PUT request with the document as JSON.

PUT /ecommerce/product/1001
{
  "name": "Zend Framework 2: From Beginner to Professional",
  "price": 50.00,
  "description": "Learn Zend Framework 2 in just a few hours!",
  "status": "active",
  "quantity": 1,
  "categories": [{
    "name": "Software"
  }],
  "tags": ["zend framework", "zf2", "php", "programming"]
}

The only thing that has been changed in the document compared to the original one, is the price.

Elasticsearch will replace any existing document with an ID of 1001 with the data that we specified. If no document already exists for that ID, it will be added. This means that you do not have to worry about whether or not the document already exists in the index, which saves you from having to perform a lookup and then update the document. If you wish, you can open up Kibana and verify that the price has been updated within the document.

As you can see, replacing documents in Elasticsearch is extremely simple.

Featured

Learn Elasticsearch today!

Take an online course and become an Elasticsearch champion!

Here is what you will learn:

  • The architecture of Elasticsearch
  • Mappings and analyzers
  • Many kinds of search queries (simple and advanced alike)
  • Aggregations, stemming, auto-completion, pagination, filters, fuzzy searches, etc.
  • ... and much more!
Elasticsearch logo
Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

Leave a Reply

Your e-mail address will not be published.