Elasticsearch basics

Anton Lytvynov
4 min readApr 7, 2021

I would like to talk about the most basics of Elasticsearch and its usage.

Elastic search is a search engine based on Apache Lucene. Apache Lucene is a high-performance, full-featured text search engine library written entirely in Java. I will not spend a lot of time to describe a process of installation. But it should be noticed that by default ES works on http://127.0.0.1:9200/ .

Also we can say that Elasticsearch is NoSQL database with Rest Api

Main notions compare to SQL

If we compare Elastic search to some SQL database here we have the main notions :

Compare Elasticsearch with SQL
  • An Index consists of one or more Types
  • a Type consists of one or more Documents
  • a Document consists of one or more Fields.

For example we have the Index heroes with the Type called hero :

  • id, name, age
  • 1, Ironman, 32
  • 2, Captain america, 27
  • 3, Spiderman, 16
  • 4, Wolverine, 36

Here every hero is a document; id, name and age are fields. Elastic search contains all documents in JSON format. In our case :

[
{
"id": 1,
"name": "Ironman",
"age": 32
},
{
"id": 2,
"name": "Captain america",
"age": 27
},
{
"id": 1,
"name": "Spiderman",
"age": 16
},
{
"id": 1,
"name": "Wolverine",
"age": 36
}
]

Installation

Look this article “Install Elasticsearch”

Elasticsearch main APIs

  • Documents CRUD Api —Create / Retrieve / Update / Delete
  • Search Api
  • Index Api
  • Cat Api
  • Clusters Api

Document CRUD Api

With this api we can manipulate with our documents. Elasticsearch uses default REST Api :

  • GET — get document
  • POST — create / update a document
  • PUT — create a new document
  • DELETE — remove document from ES.

PUT / Inserting / Indexing a new document

Inserting = Indexing document in ES

Add first hero to ES

On the left side we can see a request of indexing. And on the right is the result of indexing. Note our response contains :

  • “_index” : “heroes”
  • “_id” : “1”
  • “result” : “created”

Let’s change an age of the hero and try again our request

Now we can see “_version” : “ 2”.

GET document

Find our Ironman by index :

Try to find hero does not exist :

Try to find document which does not exist

Get only data instead of full answer :

Get only data of document instead of all reposnse

Check that Ironman document exists :

Check that a document exists

PUT / Update / Reindexing document

Update = Reindexing

To update a whole document

Reindexing a document

We can see that in this case all the document was updated, our document contains only an age.

Response

To update just only a field we need to add _update part to uri and doc to body request :

Reindexing only one field of document

DELETE a DOCUMENT

Lets delete our Ironman from ES

Now when we try to find our document we can see a field found : false

Delete a TYPE

Actually we can not delete a TYPE because it is empty.

Delete an INDEX

We can get all information of index

Index heroes information

Lets delete our index

Response : delete of index

And now if we try to get it we’ll get 404

Search Api

Imagine we have already added 4 heroes discussed earlier.

It’s easy to find them all

Simple search request

Lets try to find a hero with age 36 :

Or easier request to find by name

GET heroes/hero/_search?q=name:Spiderman

To get all index information

GET heroes/_search
{
“query”: {
“match_all”: {}
}
}

Thank you for your attention.

Thank you for your attention.

Do you need help with web or mobile development? Feel free to contact me here.

P.S. I really appreciate your like or share 🙏.

--

--

Anton Lytvynov

CEO & Founder of Lytvynov Production, Senior web developer, architect, cryptocurrencies trader, https://lytvynov-production.com