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

PUT index

To read the indexed documents, it is necessary to specify the index and the ID of the document.

GET name_index/_doc/1
GET index

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).

create doc
ID already exists
  • _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)

--

--

--

Data Engineer Elastic Stack

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to build a meme-maker with React: a beginner’s guide

WebGoat Authentication Bypass 2

for…in vs for…of in JavaScript

Learn Javascript Interview Question & Answer With Some Easy Description

Electron + React with Typescript

Thank you, Faker. Now it’s Time to Move On.

How you can structure your Express app by using Grogu.js, a lightweight Express boilerplate

Building a Chrome Extension Using React

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Iván Frías Molina

Iván Frías Molina

Data Engineer Elastic Stack

More from Medium

API Key Implementation On ELK Stack

Get started with OpenSearch — The really quick guide.

ELK (Elasticsearch, Kibana, Filebeat and Metricbeat kubernets) stack’s Automated Certificate…

Setting up request ID logging for your FastAPI application