Elasticsearch
A distributed search and analytics engine built on Apache Lucene. Powers full-text search, log analysis, and real-time analytics at scale.
What is Elasticsearch?
In short
Elasticsearch is a distributed search and analytics engine built on top of Apache Lucene that stores data as JSON documents and indexes them for fast full-text search, filtering, and aggregation. It spreads data across many nodes so you can search billions of documents in milliseconds, and it powers use cases like product search, log analysis, and observability dashboards.
What Elasticsearch Actually Is
Elasticsearch is a document database with a search engine bolted into its core. You send it JSON documents over a REST API, it stores them, and it builds an inverted index so that queries like "find every document containing the word laptop" return in milliseconds instead of scanning every row.
The thing that makes it different from a normal database is the inverted index. A relational database is optimized to fetch a row by its primary key. Elasticsearch is optimized to answer "which documents contain this term", which is exactly the question full-text search asks. It also handles fuzzy matching, relevance ranking, and synonyms, none of which a SQL LIKE query does well.
It is distributed by design. A single logical index is split into pieces called shards, and those shards are copied into replicas and spread across machines. That is what lets companies run Elasticsearch over hundreds of terabytes while still returning a query in under 100 milliseconds.
How It Works Under the Hood
Each Elasticsearch index is divided into shards. A shard is a complete, self contained Lucene index. When you index a document, Elasticsearch hashes the document id to pick a shard, so writes spread evenly across nodes. When you search, the query fans out to every shard in parallel, each shard returns its top matches, and a coordinating node merges the results. This scatter gather pattern is why search stays fast as data grows: you add nodes and shards rather than making one machine bigger.
The inverted index is the heart of it. For every term in your text, Lucene keeps a sorted list of which documents contain it and where. Searching for a word becomes a lookup in that list rather than a scan. Numeric and date fields use a different structure called BKD trees for range queries, and exact match fields use doc values stored column wise for fast sorting and aggregation.
Writes are not immediately searchable. New documents go into an in memory buffer plus a translog for durability, and Elasticsearch flushes them into a new searchable segment on a refresh interval, which defaults to one second. This is called near real time search. Segments are immutable, so deletes are just tombstones and updates are delete plus reindex, with a background merge process compacting small segments into larger ones.
Replicas serve two jobs. They protect against a node dying, and they also serve read traffic, so adding replicas increases search throughput. A write must succeed on the primary shard and is then copied to its replicas before it is acknowledged.
When to Use It and the Trade-offs
Reach for Elasticsearch when search relevance or text matching matters: an e commerce product catalog, a documentation search box, autocomplete, or matching across messy free text. It is also the standard tool for log and metrics analysis, where you ingest huge volumes of events and slice them with filters and aggregations in tools like Kibana.
It is the wrong tool as your only system of record. Elasticsearch has no real transactions, no joins across documents, and no foreign keys. If you need to atomically transfer money between two accounts, that belongs in Postgres or another transactional database. The common pattern is to keep the source of truth in a relational store and stream data into Elasticsearch as a denormalized search copy.
The other trade-offs are operational. Near real time means a one second lag between writing and seeing data by default. Memory and JVM heap tuning matters, and a badly chosen shard count is hard to change later because the shard count of an index is fixed at creation. Mapping changes often force a reindex. Running a healthy production cluster takes real attention to node roles, heap size, and shard sizing, usually keeping shards in the tens of gigabytes range.
A Concrete Example
Imagine a store with 50 million products. A user types "wireless noise canceling headphones" and expects results in under a tenth of a second, ranked so the most relevant products show first, with filters for brand and price on the side.
You index each product as a JSON document with fields for title, description, brand, and price. Elasticsearch builds an inverted index over the text fields and stores brand and price as keyword and numeric fields. The search query matches the text terms and scores each product using a relevance algorithm called BM25, which rewards documents where the search terms are frequent and rare across the catalog.
At the same time, a single request can run aggregations that count how many matching products fall under each brand and each price bucket, which is how the faceted filters on the left of a search page get their numbers. Because the work is split across shards on many nodes and run in parallel, the whole thing comes back in tens of milliseconds even over tens of millions of documents.
Where it is used in production
GitHub
Powers code and repository search across hundreds of millions of repositories using Elasticsearch clusters.
Netflix
Uses Elasticsearch for operational log analytics and to track signals across its microservices fleet.
Uber
Runs Elasticsearch for log search and observability over the events its services generate every day.
Wikipedia
Backs its on site search with Elasticsearch through the CirrusSearch extension over millions of articles.
Frequently asked questions
- Is Elasticsearch a database?
- It is a document store and a search engine in one, but it is not a transactional database. It has no ACID transactions or joins, so most teams keep their source of truth in something like Postgres and use Elasticsearch as a fast search copy of that data.
- What is the difference between Elasticsearch and Apache Lucene?
- Lucene is the underlying Java library that builds and searches the inverted index on a single machine. Elasticsearch wraps Lucene with a JSON REST API, distributes data across many nodes with sharding and replication, and handles clustering, so you get a scalable service instead of a single machine library.
- What is a shard in Elasticsearch?
- A shard is a piece of an index, and each shard is itself a complete Lucene index. Splitting an index into shards lets Elasticsearch spread data across nodes and search them in parallel. The number of primary shards is fixed when the index is created, so it has to be planned in advance.
- Why is my newly indexed document not showing up in search yet?
- Elasticsearch is near real time, not instant. New documents become searchable only after a refresh, which happens once per second by default. You can force an immediate refresh for testing, but doing it on every write hurts performance in production.
- What is the relationship between Elasticsearch, Logstash, and Kibana?
- They are the ELK or Elastic Stack. Logstash or Beats ship and transform data, Elasticsearch stores and searches it, and Kibana is the dashboard for querying and visualizing it. Together they are the most common open stack for log and metrics analysis.
Learn Elasticsearch hands-on
This page explains the idea. The full lesson lets you step through the ring as servers join and leave, read the implementation, and check yourself with a quiz. It is one of 760+ lessons in the System Design Masterclass, from your first API call to distributed consensus. Eleven Foundation lessons are free, no signup. Lifetime access is ₹499 in India or $7.99 worldwide, one payment, no subscription.
Related lessons
Lessons that touch on Elasticsearch as part of a larger topic.
Search Engines (Elasticsearch, Solr)
Distributed search engines that power product search, log analysis, and real-time analytics at massive scale
intermediate · database types storage
ELK Stack
Elasticsearch, Logstash, and Kibana, the open-source log management stack used by thousands of organizations
intermediate · observability monitoring
Inverted Index
The data structure powering every search engine, mapping words to the documents that contain them
intermediate · database types storage
See also
Related glossary terms you might want to look up next.
Index
A data structure that speeds up database lookups. Like the index at the back of a book that lets you jump to the right page instead of reading every page.
NoSQL
Databases that don't use traditional table-based relational models. Includes document stores, key-value, graph, and column-family databases.
Distributed Tracing
Tracking a request as it flows through multiple services in a distributed system. Each service adds its trace, creating a full picture of the request journey.
Redis
An in-memory data store used as a cache, message broker, and database. Blazing fast because everything lives in RAM.
Memcached
A simple, high-performance distributed memory caching system. Stores key-value pairs in RAM. Simpler than Redis but less feature-rich.
Document Database
A NoSQL database that stores data as flexible JSON-like documents. MongoDB and CouchDB let each document have a different structure.