Elasticsearch Document CRUD
Tags: elasticsearch, postman, crud, search
Published: 2020-09-21 16:46:03

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](https://dev.to/dendihandian/elk-stack-local-development-using-docker-elk-6k7) 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
```

with the raw body request:

```json
{
    "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:

```json
{
    "_index": "store",
    "_type": "product",
    "_id": "Lt9tsXQBeCj-V4WaE80C",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}
```

<img src="https://i.ibb.co/w6zHH3g/create.png" alt="create" border="0">

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
```

The response would be like this:

```json
{
    "_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)"
    }
}
```

<img src="https://i.ibb.co/ZGDPVph/read.png" alt="read" border="0">

## 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
```

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

```json
{
    "doc": {
        "name": "{{$randomProductName}}",
        "slug": "{{$randomLoremSlug}}"
    }
}
```

<img src="https://i.ibb.co/cTWKh4V/update-partial.png" alt="update-partial" border="0">

### Override Update

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

```
http://localhost:9200/store/product/Lt9tsXQBeCj-V4WaE80C
```

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

```json
{
    "name": "{{$randomFullName}}",
    "sold": true
}
```

<img src="https://i.ibb.co/GsmND97/update-override.png" alt="update-override" border="0">

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
```

<img src="https://i.ibb.co/HLvqSYX/delete.png" alt="delete" border="0">

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
```