DEV Community

Kalyan Kumar
Kalyan Kumar

Posted on

How does ElasticSearch work?

ElasticSearch is a search engine technology used by many companies for use-cases like: full-text search, autocomplete suggestions, data analytics, etc.

It is a document-oriented database. The high level architecture of ElasticSearch is as follows (bottom-up):

Document

Documents are the basic units of information you can store in ElasticSearch. You store the data in the form of a JSON document. Every document has a unique ID which is used for reverse indexing the data (discussed at the end).
Image description

Index

An index is a collection of the similar type documents. An index is analogous to the database in relational databases. When you query in ElasticSearch, you query against a particular index.
Image description

Node and Cluster

An ElasticSearch cluster is a collection of nodes connected together.
Image description

Shard

It is not necessary that an index in ElasticSearch cluster need to be present only on a single node. It could be broken down into parts and shared among multiple nodes to achieve horizontal scalability. Each part of an index present on a node is called a Shard.
Example:
Image description
users index has 10k documents which are shared between node1 and node2. Each part of 5k documents is called a shard.

Now that we understand high level architecture of ElasticSearch, let's have a look at how it internally works.

Reverse Indexing

ElasticSearch uses a special data structure called reverse index. Every term in the data is indexed against the document IDs of documents containing the term.
Let's understand this through an example below:
Image description

Consider 2 documents as stated above with IDs: 1 and 2. When these documents are indexed(loaded into the index), every term from the document is taken and it is mapped to the document ID. When you query for a certain term, it is like a lookup in the dictionary. This is what primarily makes ElasticSearch very fast.

More articles to follow in this series of ElasticSearch articles. Please comment down if you want me to write an article about a specific topic.

Top comments (0)