In this blog, I am going to discuss about Elasticsearch. What are the benefits of using it as a database and How to use it.
Elasticsearch
Elasticsearch is a highly scalable open-source full-text search and analytics engine. It allows you to store, search, and analyse big volumes of data quickly and in near real time.
Elasticsearch provides near real-time search and analytics for all types of data. Whether you have structured or unstructured text, numerical data, or geospatial data, it can efficiently store and index it in a way that supports fast searches. You can go far beyond simple data retrieval and aggregate information to discover trends and patterns in your data. And as your data and query volume grows, the distributed nature of Elasticsearch enables your deployment to grow seamlessly right along with it.
Few terminologies are:
- Node: Node is a single instance that is part of your cluster, stores the data and participates in the cluster indexing and search capabilities.
- Cluster: Cluster is a group of nodes that holds the data and provides indexing and search capabilities across all nodes.
- Index: A type is a logical category/partition of your index. It is defined for documents that have a set of common fields. It has been removed after version 7.0 of Elasticsearch.
- Shard: Shard is basically a Lucene index. It contains the various documents and various data structure that help in searching. Lucene is a full-text library which helps easy searching.
CRUD operation applied on Elasticsearch:
First we should check that Elasticsearch has started, by default the port number is 9200
GET localhost:9200
{
"acknowledged": true,
{
"name": "t9mGYu5",
"cluster_name": "elasticsearch",
"cluster_uuid": "xq-6d4QpSDa-kiNE4Ph-Cg",
"version": {
"number": "5.5.0",
"build_hash": "260387d",
"build_date": "2017-06-30T23:16:05.735Z",
"build_snapshot": false,
"lucene_version": "6.6.0"
},
"tagline": "You Know, for Search"
}
Now we will add a document
POST localhost:9200/accounts/person/1
{
"name" : "Ram",
"job_description" : "Software Engineer"
}
Response after creating a document is
{
"_index": "accounts",
"_type": "person",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
Now we will fetch a document from Elasticsearch, in this we can fetch the whole record or can specify any id number as well.
GET localhost:9200/accounts/person/1
Response after fetching a document with Id is
{
"_index": "accounts",
"_type": "person",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "Ram",
"job_description": "Software Engineer"
}
}
We can update the document
POST localhost:9200/accounts/person/1/_update
{
"doc":{
"job_description" : "Senior Software Engineer"
}
}
Response after updating the record
{
"_index": "accounts",
"_type": "person",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "Ram",
"job_description": "Senior Software Engineer"
}
}
We can also delete a record
DELETE localhost:9200/accounts/person/1
Response after deleting a record
{
"found": true,
"_index": "accounts",
"_type": "person",
"_id": "1",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
Hope this blog will help you.
Happy Blogging!
References:-
