How Elasticsearch Works: Index, Documents & Search Process
How Elasticsearch Works – Understanding Indexing, Documents, and Search
In the previous lesson, you learned why Elasticsearch is needed and how it overcomes the limitations of traditional databases.
Now it's time to understand what happens behind the scenes.
Have you ever wondered:
- How does Elasticsearch find information so quickly?
- Where is the data stored?
- What exactly is an Index?
- What is a Document?
- How does a search happen in just a few milliseconds?
By the end of this lesson, you'll understand the core building blocks of Elasticsearch and how they work together.
The Journey of Data in Elasticsearch
Whenever new data is added to Elasticsearch, it follows a simple process:
Application
│
▼
Send Data
│
▼
Elasticsearch
│
▼
Store as a Document
│
▼
Create an Index
│
▼
Data Becomes Searchable
Instead of searching every document one by one, Elasticsearch creates a special searchable structure called an index, allowing it to locate information much faster.
What is a Document?
A Document is the smallest unit of data stored in Elasticsearch.
Think of a document as one complete record.
Real-Life Example
Imagine an online shopping website.
One product might contain:
- Product Name
- Price
- Category
- Brand
- Rating
Each product is stored as one document.
For example:
{
"product_name": "Wireless Mouse",
"brand": "Logitech",
"price": 999,
"category": "Electronics",
"rating": 4.7
}
This entire JSON object is called a document.
Why Does Elasticsearch Use JSON?
Elasticsearch stores data in JSON (JavaScript Object Notation).
JSON is:
- Easy for humans to read.
- Easy for computers to process.
- Supported by most programming languages.
Almost every application that communicates with Elasticsearch sends and receives JSON data.
What is an Index?
An Index is a collection of related documents.
Think of it as a folder that stores similar types of information.
Example
Suppose you're building an e-commerce application.
Instead of putting everything into one place, you organize the data like this:
Products Index
├── Laptop
├── Mouse
├── Keyboard
Customers Index
├── Customer 1
├── Customer 2
Orders Index
├── Order 101
├── Order 102
Each index contains documents related to a specific category.
Real-Life Analogy
Imagine a library.
The library itself contains many sections:
- Science
- History
- Fiction
- Technology
Each section is like an Index.
Inside every section are many books.
Each book is like a Document.
Library
│
├── Science (Index)
│ ├── Book 1 (Document)
│ ├── Book 2
│
├── Fiction (Index)
│ ├── Book 1
│ ├── Book 2
This organization makes it much easier to locate information.
What Happens During Indexing?
Indexing is the process of preparing data so it can be searched efficiently.
When a new document is added, Elasticsearch:
Step 1
Receives the JSON document.
↓
Step 2
Analyzes the text.
↓
Step 3
Breaks the text into smaller searchable pieces called tokens.
↓
Step 4
Stores these tokens in its search index.
↓
Step 5
Makes the document searchable.
Example of Indexing
Suppose you add this product:
{
"product_name": "Wireless Bluetooth Headphones"
}
Elasticsearch analyzes the text and breaks it into words such as:
Wireless
Bluetooth
Headphones
These words are stored in the index.
Now users can search using words like:
- Wireless
- Bluetooth
- Headphones
and Elasticsearch can quickly find the matching document.
What Happens When You Search?
Suppose a customer searches for:
Bluetooth Headphones
Elasticsearch follows these steps:
User Searches
│
▼
Analyze Search Text
│
▼
Look Inside the Index
│
▼
Find Matching Documents
│
▼
Calculate Relevance Score
│
▼
Display Results
Notice that Elasticsearch searches the index, not every document individually.
This is one of the main reasons it is so fast.
Why Is Searching an Index Faster?
Imagine you have 50,000 books.
Without an Index
You open every book until you find the word.
This could take hours.
With an Index
You first check the alphabetical index.
It immediately tells you which books contain the word.
You go directly to those books.
Elasticsearch works in a similar way.
Understanding Tokens
When Elasticsearch analyzes text, it divides it into smaller pieces called tokens.
Example:
Learning Elasticsearch is Easy
After tokenization:
Learning
Elasticsearch
Easy
Common words like is, the, and a may be ignored, depending on the analyzer being used.
This helps improve search performance and relevance.
What is an Analyzer?
An Analyzer prepares text before it is indexed or searched.
It can:
- Convert text to lowercase.
- Remove punctuation.
- Split sentences into words.
- Remove common words.
- Apply language-specific processing.
For example:
Input:
Wireless Bluetooth Headphones
Output:
wireless
bluetooth
headphones
This allows searches to work even if the user types:
BLUETOOTH HEADPHONES
The case difference doesn't affect the search results.










