Searching Data in Elasticsearch: Queries, Search & Filters
Searching Data in Elasticsearch
So far, you've learned how to:
- Store data in Elasticsearch.
- Organize data using indices and documents.
- Perform CRUD (Create, Read, Update, Delete) operations.
Now comes the most exciting part searching.
Searching is the primary reason Elasticsearch exists. While traditional databases can search data, Elasticsearch is specifically designed to search through massive amounts of information quickly and accurately.
Whether you're searching for a product on Amazon, a movie on Netflix, or an article on a news website, Elasticsearch helps deliver relevant results in milliseconds.
By the end of this lesson, you'll understand:
- How Elasticsearch performs searches.
- What a query is.
- Different types of search queries.
- How Elasticsearch decides which results are most relevant.
- The difference between searching and filtering.
What is a Search Query?
A search query is simply a request asking Elasticsearch to find specific information.
Whenever you type something into a search box, you're creating a search query.
For example:
- Running Shoes
- Bluetooth Speaker
- Python Programming Book
These words are sent to Elasticsearch, which then searches through its indices to find matching documents.
Think of a search query as asking a librarian:
"Can you find books about space exploration?"
The librarian searches the library catalog instead of checking every book individually.
Elasticsearch works in a similar way.
How Does Elasticsearch Search So Quickly?
Let's understand what happens behind the scenes.
Suppose a customer searches for:
Wireless Mouse
Elasticsearch follows these steps:
User Types Search
│
▼
Receive Search Query
│
▼
Analyze the Words
│
▼
Search the Index
│
▼
Find Matching Documents
│
▼
Rank Results by Relevance
│
▼
Display Results
Notice that Elasticsearch searches the index, not every document.
This is why searches are extremely fast.
Understanding Relevance
Imagine searching an online bookstore for:
Python Programming
There might be hundreds of books containing the word Python.
Should every book appear in random order?
No.
Instead, Elasticsearch calculates which books are most relevant to your search.
For example:
Book | Relevance |
| Python Programming for Beginners | ⭐⭐⭐⭐⭐ |
| Mastering Python | ⭐⭐⭐⭐ |
| Introduction to Programming | ⭐⭐⭐ |
The books that best match your search appear first.
This ranking process is one of Elasticsearch's biggest strength.
Full-Text Search
One of Elasticsearch's most powerful features is Full-Text Search.
Instead of looking for an exact word, Elasticsearch tries to understand what you're searching for.
Suppose a product is stored as:
Wireless Bluetooth Headphones
A customer searches:
Bluetooth Headset
Although the words aren't exactly the same, Elasticsearch may still return the correct product because it analyzes the text intelligently.
This creates a much better search experience than simple keyword matching.
Match Query
The most common search type in Elasticsearch is the Match Query.
A Match Query searches analyzed text fields.
Imagine your products index contains these products:
Product Name |
| Gaming Laptop |
| Office Laptop |
| Wireless Mouse |
| Gaming Keyboard |
A user searches:
Gaming
Elasticsearch returns:
- Gaming Laptop
- Gaming Keyboard
because both documents contain the searched word.
This type of search is flexible and works well for normal text searches.
Term Query
A Term Query is different.
It looks for an exact value.
Imagine a field called:
Brand
Possible values:
- Dell
- HP
- Lenovo
If you search for:
Dell
Elasticsearch returns only documents where the brand is exactly Dell.
Term queries are commonly used with fields such as:
- Product IDs
- Brand names
- Status values
- Country codes
Unlike Match Queries, Term Queries do not analyze the text before searching.
Match Query vs Term Query
Let's compare them.
Suppose a document contains:
Apple iPhone 16 Pro
Match Query
Searching:
Apple
or
iPhone
can successfully find the document because Elasticsearch analyzes the text.
Term Query
Searching:
Apple
will only work if the field stores Apple as an exact value.
If the field contains the complete phrase:
Apple iPhone 16 Pro
the search may not return the expected result.
This is why choosing the correct query type is important.
Multi Match Query
Sometimes you want to search across multiple fields.
Imagine a movie database.
Each movie has:
- Title
- Description
- Genre
A user searches:
Adventure
Should Elasticsearch only search the title?
No.
It should search:
- Movie Title
- Movie Description
- Genre
A Multi Match Query searches several fields at the same time.
This helps users find relevant results even when the keyword appears in different parts of a document.
Searching Multiple Words
Suppose a customer searches:
Gaming Laptop
Elasticsearch analyzes both words separately.
It searches for:
- Gaming
- Laptop
Then combines the results to find the best matching products.
This makes searches much more flexible than exact phrase matching.
What Happens if No Results Are Found?
Suppose a customer searches:
Purple Dragon Laptop
No such product exists.
Instead of displaying incorrect information, Elasticsearch simply returns:
No matching documents found.
Many applications then display messages such as:
- No products found.
- Try another keyword.
- Did you mean...?
These features improve the user experience.
Searching with Partial Words
Modern applications often show results while you're still typing.
For example:
User types:
Lap
Possible suggestions:
- Laptop
- Laptop Bag
- Laptop Stand
This feature is called Autocomplete.
Elasticsearch supports autocomplete using specialized indexing techniques, allowing users to find information more quickly.
Real-World Example
Imagine searching on Netflix.
You type:
Avengers
Immediately, Netflix displays:
- Avengers
- Avengers: Endgame
- Avengers: Infinity War
This fast response is possible because Elasticsearch (or similar search technologies) uses optimized search indices instead of scanning every movie individually.
Search vs Filter
Many beginners confuse searching and filtering.
Although they seem similar, they serve different purposes.
Searching
Searching tries to find the most relevant information.
Example:
Search:
Gaming Laptop
Elasticsearch looks for documents that best match those words.
Filtering
Filtering narrows down the search results using specific conditions.
Example:
After searching for laptops, a customer selects:
- Brand: Dell
- Price: Under ₹60,000
- Rating: Above 4 Stars
These conditions filter the results.
Unlike searching, filtering usually does not calculate relevance scores.
Example Workflow
Imagine an online shopping website.
A customer searches:
Laptop
10,000 products match.
The customer then applies filters:
- Dell
- 16 GB RAM
- SSD Storage
- Under ₹70,000
Now only 45 products remain.
Searching finds possible matches.
Filtering narrows those matches.
Why Is Elasticsearch Better at Search?
Traditional databases mainly compare values.
Elasticsearch goes much further by:
- Understanding analyzed text.
- Ranking results by relevance.
- Supporting full-text search.
- Searching across multiple fields.
- Handling millions of documents efficiently.
- Returning results in milliseconds.
This is why many modern applications use Elasticsearch for their search functionality.
Important Notes
- Use Match Queries for normal text searches.
- Use Term Queries when searching for exact values.
- Use Multi Match Queries when searching across multiple fields.
- Filtering is used to narrow results after or alongside a search.
- Elasticsearch ranks search results automatically based on relevance.










