Replacing Documents
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.
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!