Elasticsearch from 0 to sky: CRUD- Chapter 2

JSON Objects
Elasticsearch works with data structures represented as JSON objects.
- JSON documents are saved with a unique ID
- In Elasticsearch the documents are indexed in index
{
"title": "Elasticsearch from 0 to sky: CRUD",
"tecnology": "Elasticsearch",
"date": "23/02/2021",
"author": {
"first_name": "Ivan",
"last_name": "Frias",
"company": "CIVIR"
}
}
Indexing documents
Index API is used to index documents, we can use a curl.
It is necessary to indicate the name of the index (1), endpoint (2) and the ID (3)
The ID can be autogenerated if not specified.
(1) (2) (3)
$ curl -X PUT "elasticsearch:9200/name_index/_doc/1" -H 'Content-Type: application/json' -d'
{
"title": "Elasticsearch from 0 to sky: CRUD",
"tecnology": "Elasticsearch",
"date": "23/02/2021",
"author": {
"first_name": "Ivan",
"last_name": "Frias",
"company": "CIVIR"
}
}
An index called “name_index” with an ID “1” will be created
- Kibana now incorporates a console where you can make requests much more visual, we can also use Postman or any other program.
- The indexes will be created automatically when you index a document.
The indexes can be created manually in the following way:
$ curl -X PUT "elasticsearch:9200/name_index"
{
"settings":{
},
"mappings":
{
}
}
- Add doc with ID 1

To read the indexed documents, it is necessary to specify the index and the ID of the document.
GET name_index/_doc/1

Endpoints
There are different endpoints to perform CRUD operations (Create, Read, Update, Delete).
- _create
Use _create to create documents, if the ID exists it will give a 409 error, otherwise you use endpoint (as in the previous example) it will be created and updated when you use the same ID (index).


- _update
Use _update to update documents, the _version field will increment.

- delete
Use delete to delete a document, response code is 200 when the document has been found and deleted.

Bulk API
- _bulk
It allows to carry out many operations with a single call, optimizing the result with an increase in speed.
Allows create, index, update and delete actions.

- _mget
Allows to return multiple documents by ID (multi get)
