Elasticsearch basics
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 :
- 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
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 :
Get only data instead of full answer :
Check that Ironman document exists :
PUT / Update / Reindexing document
Update = Reindexing
To update a whole document
We can see that in this case all the document was updated, our document contains only an age.
To update just only a field we need to add _update part to uri and doc to body request :
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
Lets delete our 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
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 🙏.