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 :
"name": "Captain america",
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”.
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
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
To get all index information
Thank you for your attention
Thank you for your attention. If you have any questions or advices please feel free to contact me. I will be glad to help you.