loading...

Elasticsearch Document CRUD

dendihandian profile image Dendi Handian Updated on ・3 min read

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 called document and it stored inside index and type. 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 it here 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 the index as store and the type as product. 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 the POST method to the URL:

http://localhost:9200/store/product
Enter fullscreen mode Exit fullscreen mode

with the raw body request:

{
    "name": "{{$randomProductName}}",
    "slug": "{{$randomLoremSlug}}",
    "description": "{{$randomLoremText}}",
    "quantity": {{$randomInt}},
    "price": {{$randomPrice}},
    "created_at": "{{$randomDatePast}}"
}
Enter fullscreen mode Exit 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 mode Exit fullscreen mode

create

I got the Lt9tsXQBeCj-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 using GET method, my URL would be like this:

http://localhost:9200/store/product/Lt9tsXQBeCj-V4WaE80C
Enter fullscreen mode Exit 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 mode Exit 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 using POST methods and the URL is:

http://localhost:9200/store/_update/Lt9tsXQBeCj-V4WaE80C
Enter fullscreen mode Exit fullscreen mode

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

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

update-partial

Override Update

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

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

The request body will only contain the name and the new field sold:

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

update-override

You can check both update changes by using the previous READ Document.

DELETE Document

By using DELETE methods to the URL:

http://localhost:9200/store/product/Lt9tsXQBeCj-V4WaE80C
Enter fullscreen mode Exit 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 mode Exit fullscreen mode

Discussion

pic
Editor guide