Elasticsearch is a restful-based service. Any resource that has a restful interface will have at least GET, POST, PUT and DELETE request methods, which means we can perform CRUD (create, read, update, delete) here. The resource item or entity in the elasticsearch is calleddocument
and it stored insideindex
andtype
. So, let's do an example of this CRUD in elasticsearch.
Prerequisites to code along
I assume you have set up the elasticsearch server already. If not, then you can have a look for how to set up ithere using docker, as well as access it using Postman or Curl. I will use Postman here for simplicity's sake.
Our Index and Type
For this post example, let's define theindex
asstore
and thetype
asproduct
. So, the basic URL for our document endpoints would be like this:http://localhost:9200/store/product
CREATE Document
We can create a document directly without creating the index first. To create a document, we can use thePOST
method to the URL:
http://localhost:9200/store/product
with the raw body request:
{"name":"{{$randomProductName}}","slug":"{{$randomLoremSlug}}","description":"{{$randomLoremText}}","quantity":{{$randomInt}},"price":{{$randomPrice}},"created_at":"{{$randomDatePast}}"}
If you successfully create the document, the response would be like this:
{"_index":"store","_type":"product","_id":"Lt9tsXQBeCj-V4WaE80C","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}
I got theLt9tsXQBeCj-V4WaE80C
as the value of_id
of the created document. We will use this ID for the READ below.
READ Document
We can use the_id
value for getting the created document. By usingGET
method, my URL would be like this:
http://localhost:9200/store/product/Lt9tsXQBeCj-V4WaE80C
The response would be like this:
{"_index":"store","_type":"product","_id":"Lt9tsXQBeCj-V4WaE80C","_version":1,"_seq_no":1,"_primary_term":1,"found":true,"_source":{"name":"Practical Steel Salad","slug":"dolorem-et-ut","description":"Quia ullam fugit ut sit repudiandae repellat porro dolor. Quis quia autem ea assumenda esse quia enim. Enim expedita ut ex inventore facilis unde dolorem. Qui aut officiis facilis aperiam voluptas dolore.","quantity":795,"price":635.76,"created_at":"Tue Dec 31 2019 11:26:23 GMT+0700 (Western Indonesia Time)"}}
UPDATE Document
There are two types of updating the document here, partial update or override update. Partial update used for updating single or multiple fields. For this example, we can update only the name or only price and quantity. Override update used for updating the entire document regardless of what fields it has, even you can change the document source structure to be different.
Partial Update
We can do the partial-update usingPOST
methods and the URL is:
http://localhost:9200/store/_update/Lt9tsXQBeCj-V4WaE80C
And for the body request, we only update the name and the slug like this:
{"doc":{"name":"{{$randomProductName}}","slug":"{{$randomLoremSlug}}"}}
Override Update
We can do the override-update usingPUT
methods with this URL:
http://localhost:9200/store/product/Lt9tsXQBeCj-V4WaE80C
The request body will only contain the name and the new fieldsold
:
{"name":"{{$randomFullName}}","sold":true}
You can check both update changes by using the previous READ Document.
DELETE Document
By usingDELETE
methods to the URL:
http://localhost:9200/store/product/Lt9tsXQBeCj-V4WaE80C
Your document should be gone, you can check it using the READ Document.
Have fun exploring elasticsearch.
versions used: - elasticsearch: 7.9.1- postman: v7.32.0
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse