Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Dendi Handian
Dendi Handian

Posted on • Edited on

     

Elasticsearch Document CRUD

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
Enter fullscreen modeExit fullscreen mode

with the raw body request:

{"name":"{{$randomProductName}}","slug":"{{$randomLoremSlug}}","description":"{{$randomLoremText}}","quantity":{{$randomInt}},"price":{{$randomPrice}},"created_at":"{{$randomDatePast}}"}
Enter fullscreen modeExit fullscreen mode

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}
Enter fullscreen modeExit fullscreen mode

create

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
Enter fullscreen modeExit fullscreen mode

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)"}}
Enter fullscreen modeExit fullscreen mode

read

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
Enter fullscreen modeExit fullscreen mode

And for the body request, we only update the name and the slug like this:

{"doc":{"name":"{{$randomProductName}}","slug":"{{$randomLoremSlug}}"}}
Enter fullscreen modeExit fullscreen mode

update-partial

Override Update

We can do the override-update usingPUT methods with this URL:

http://localhost:9200/store/product/Lt9tsXQBeCj-V4WaE80C
Enter fullscreen modeExit fullscreen mode

The request body will only contain the name and the new fieldsold:

{"name":"{{$randomFullName}}","sold":true}
Enter fullscreen modeExit fullscreen mode

update-override

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
Enter fullscreen modeExit fullscreen mode

delete

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
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Data Engineer - Building Data Lake & Modern Data Architecture
  • Location
    Jakarta
  • Education
    B.S. Computer Science
  • Work
    Data Engineer at Media Production Company
  • Joined

More fromDendi Handian

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp