Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Elasticsearch Architecture

1. Introduction

Elasticsearch is a distributed, RESTful search and analytics engine capable of solving a growing number of use cases. It enables you to store, search, and analyze large volumes of data quickly and in near real-time. This tutorial will provide a comprehensive guide to understanding the architecture of Elasticsearch.

2. Core Concepts

Before diving into the architectural components, it's crucial to understand some core concepts in Elasticsearch:

  • Document: The basic unit of information that can be indexed. A document is expressed in JSON format.
  • Index: A collection of documents that have somewhat similar characteristics.
  • Shard: A subset of an index. Each index is split into multiple shards for distribution.
  • Replica: A copy of a shard. Replicas provide high availability and fault tolerance.
  • Node: A single server that is part of the Elasticsearch cluster, which stores data and participates in the cluster’s indexing and search capabilities.
  • Cluster: A collection of one or more nodes that together holds the entire data and provides federated indexing and search capabilities across all nodes.

3. Elasticsearch Cluster

An Elasticsearch cluster is a group of nodes that work together to provide indexing and search capabilities. Each node in the cluster holds data and participates in the cluster's operations. The cluster is identified by a unique name, and all nodes must have the same cluster name to be part of the cluster.

Example: A simple cluster setup with three nodes:
                    node.name: node-1
                    cluster.name: my-cluster

                    node.name: node-2
                    cluster.name: my-cluster

                    node.name: node-3
                    cluster.name: my-cluster
                

4. Nodes and Their Roles

Nodes in an Elasticsearch cluster can serve different roles:

  • Master Node: Responsible for cluster-wide settings and management. It keeps track of all nodes and indices in the cluster.
  • Data Node: Stores data and executes data-related operations such as CRUD, search, and aggregations.
  • Ingest Node: Preprocesses documents before indexing.
  • Coordinating Node: Routes requests and handles the coordination of search and indexing operations.
Example: Configuring nodes with specific roles:
                    node.name: master-node
                    node.master: true
                    node.data: false
                    node.ingest: false

                    node.name: data-node
                    node.master: false
                    node.data: true
                    node.ingest: false

                    node.name: ingest-node
                    node.master: false
                    node.data: false
                    node.ingest: true
                

5. Sharding and Replication

Elasticsearch uses sharding and replication to distribute data and ensure high availability:

  • Sharding: Each index is divided into shards. A shard is a fully functional and independent "index" that can be hosted on any node in the cluster.
  • Replication: Each shard can have multiple replicas. Replicas are copies of the shard and provide redundancy and failover.
Example: Creating an index with 3 shards and 2 replicas:
                    PUT /my-index
                    {
                        "settings": {
                            "number_of_shards": 3,
                            "number_of_replicas": 2
                        }
                    }
                

6. Elasticsearch Indexing

Indexing is the process of adding data to Elasticsearch. When you index a document, Elasticsearch stores it in the specified index and makes it searchable.

Example: Indexing a document:
                    POST /my-index/_doc/1
                    {
                        "title": "Elasticsearch Architecture",
                        "description": "A comprehensive guide to understanding Elasticsearch architecture."
                    }
                

7. Searching in Elasticsearch

Searching is one of the core functionalities of Elasticsearch. You can perform simple searches using the REST API.

Example: Performing a search query:
                    GET /my-index/_search
                    {
                        "query": {
                            "match": {
                                "title": "Elasticsearch"
                            }
                        }
                    }
                

8. Conclusion

Understanding the architecture of Elasticsearch is essential to effectively using it for search and analytics. This tutorial covered the core concepts, cluster and node setup, sharding and replication, and basic operations like indexing and searching. With these foundations, you can start exploring more advanced features and configurations of Elasticsearch.