
Laravel + Elasticsearch: Smart Search for E-commerce in 2025
ScaleupAlly Team | July 3, 2025 , 15 min read
Table Of Content
In this fast moving world, where we are in the network of AI, each potential user wants a system where their digital business can run smoothly. When we say digital business, we mean a business that is running on the internet where search functionality is the most important part of the online business.
Whether it’s a small business based on education, service or it’s a large scale business based on e-commerce, applying search is the most critical and useful feature while delivering the service to the user. Users are not only looking for the product, they are looking for the product that is relevant to their needs and wants.
So based on searching, we need to build a search system that is fast, accurate, and gives the results quickly. That’s where we talk about Elasticsearch. Integrating Elasticsearch with Laravel is a great way to build a search system that is not only fast but also accurate and gives the results in seconds.
This blog will walk you through the process of integrating Elasticsearch with Laravel to build a powerful, real-time search system for your e-commerce app.
Key Takeaways
- Understand how Elasticsearch connects with Laravel and enhances the search functionality.
- Learn how to set and configure Elasticsearch with Laravel using Docker and Laravel Sail.
- Know how to use Laravel model observers for automatic indexing.
- Introduce real-time full-text search capabilities in your e-commerce app.
- Visualize real-world e-commerce use cases and explore advanced Elasticsearch features.
What is Elasticsearch?
- What is Elasticsearch?
- How Does it Work?
- How Does it Differ From Normal Search Functionality?
- Why Use Elasticsearch with Laravel?
- Setting Up Laravel with Elasticsearch
- Creating the Elasticsearch Service Class
- Laravel Model Observers for Indexing
- Indexing and Searching Data
- Optional Method: ElasticLens
- Advanced Integration Concepts
- Real-World Use Cases
- Conclusion
- Frequently Asked Questions
Elasticsearch is a distributed and open-source search engine that helps you quickly find and analyze large amounts of data. To filter out instant suggestions in modern applications, Elasticsearch is a great tool. It has fast and powerful search capabilities as it is designed for horizontal scalability and reliability. It has great ability to handle full-text search, structured search, and analytics.
For example, imagine you have a huge library of books, and you want to find all books that mention “environment” — not just in the title, but anywhere in the content. Doing that manually, you don’t know how many books are there and how much time it will take. Elasticsearch lets you search through all that text in milliseconds. It’s commonly used for e-commerce product searches in websites, logs and error tracking, monitoring servers, dashboards, analyzing data in Kibana, etc.
Key Features of Elasticsearch:
- Full-text search with better relevance and performance.
- Real-time search and indexing.
- Powerful domain-specific language for complex queries.
- Horizontal scalability and enhanced architecture.
- In-built analytics and aggregated data.
How Does it Work?
1. You put data into it (Indexing)
- Elasticsearch stores data in a special format that makes searching really fast. This process is called indexing.
2. It organizes data into structures
- Data is stored in Indexes (like folders).
- Each piece of data is a Document (like a file).
- Documents have Fields (like columns in a spreadsheet).
3. You search using queries
- Send a search query (in JSON) to Elasticsearch like find all documents with the word ‘climate’.
4. It ranks results
- Results are not just matched — they’re also scored based on relevance. So if a post mentions “climate change” 10 times, it might rank higher than one that mentions it once.
How Does it Differ From Normal Search Functionality?
Parameter | Normal Search | ElasticSearch |
---|---|---|
Speed & Performance | Scans rows with LIKE queries - slows down with data growth. | Uses inverted indexes - ultra-fast even with millions of records. |
Smart Matching | Matches exact words only. | Ranks results by relevance (TF-IDF, BM25). |
Full-Text Capabilities | No support for typos, word forms, or stemming. | Supports stemming, fuzzy search, and synonyms. |
Complex Queries | Basic filtering with WHERE and LIKE. | Supports filters, range, geo search, and aggregations. |
Analytics | Not built for analytics. | Real-time search analytics with tools like Kibana. |
Schema Flexibility | Requires rigid table structure. | Flexible JSON-based schema - easily adapts to changes. |
Scalability | Tied to a single DB - hard to scale. | Distributed across nodes - built for scaling big data. |
Why Use Elasticsearch with Laravel?
Laravel Elasticsearch gives the surety to provide a powerful and flexible solution for implementing effective search capabilities. By providing robust features of Elasticsearch, user experience can be enhanced and also improved data accessibility. It provides full support for relational databases like MySQL, PostgreSQL, etc. It has a structured format for document indexing. Integrating Elasticsearch with Laravel can boost the performance of the application and helps in enabling features like –
- Real-time and full-text search.
- Filtered search results.
- Autocomplete and fast search suggestions.
- Ensures scalability and flexibility.
In short, Elasticsearch with Laravel helps in delivering amazing search experiences to the user.
Setting Up Laravel with Elasticsearch
Let’s start Elasticsearch setting by creating a development environment using Laravel Sail and Docker. Laravel Sail is a simple, lightweight command-line interface for developing and running Laravel applications using Docker.
Step 1: Install Laravel with Sail
Run below commands –
curl -s "https://laravel.build/laravel-elasticsearch" | bash
cd laravel-elasticsearch
./vendor/bin/sail up
Step 2: Add Elasticsearch to Sail
Modify your file – docker-compose.yml to include an Elasticsearch container:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.7.0
environment:
- discovery.type=single-node
- xpack.security.enabled=false
ports:
- "9200:9200"
# Then restart Sail:
./vendor/bin/sail down
./vendor/bin/sail up -d
Step 3: Install Elasticsearch PHP Client
./vendor/bin/sail composer require elasticsearch/elasticsearch
Creating the Elasticsearch Service Class
Elasticsearch service class is a custom class of Laravel. This class organizes and helps in encapsulating all interactions with Elasticsearch. To interact with Elasticsearch, we will create a dedicated service class.
<?php
use Elasticsearch\ClientBuilder;
class ElasticsearchService
{
protected $client;
public function __construct()
{
$this->client = ClientBuilder::create()
->setHosts(['elasticsearch:9200'])
->build();
}
public function index(string $index, array $body)
{
return $this->client->index([
'index' => $index,
'body' => $body,
]);
}
public function search(string $index, array $query)
{
return $this->client->search([
'index' => $index,
'body' => $query,
]);
}
public function bulk(array $params)
{
return $this->client->bulk($params);
}
}
Laravel Model Observers for Indexing
Now the question comes: What are model observers? So, model observers help in keeping your Elasticsearch index in sync with your database. Let’s create an observer for a Product model by running the below command –
PS C:\Users\pc> php artisan make:observer ProductObserver --model=Product
Then implement the observer methods and register it in AppServiceProvider:
use App\Models\Product;
use App\Observers\ProductObserver;
public function boot()
{
Product::observe(ProductObserver::class);
}
Indexing and Searching Data
Once your service and observer are set, it’s very easy for you to manually or automatically index data.
Manual Indexing
$es = app(ElasticsearchService::class);
$es->index('products', [
'id' => 1,
'name' => 'Blue Sneakers',
'price' => 89.99
]);
Search with Full-Text Query
$query = [
'query' => [
'match' => [
'name' => 'sneakers'
]
]
];
$results = $es->search('products', $query);
Optional Method: ElasticLens
Here is the alternative quick setup i.e., Elasticlens. For those developers who don’t want to use Docker, Elasticlens is a good choice. It’s a Laravel package that simplifies the integration process with Eloquent-style models.
Advanced Integration Concepts
These are some of the advanced features that you can implement in your Laravel application using Elasticsearch:
- Bulk Indexing: When dealing with large datasets, use the Bulk API to improve performance.
- Highlighting: It shows the matched terms in the results.
- Custom Mapping: For better search accuracy, define analyzers and tokenizers.
- Query Builder Wrappers: You can use Laravel-style syntax to build complex queries and also reusable search logic.
- Fuzzy Search: It finds the similar words and also the words that are close to the search query.
Real-World Use Cases
1. Instant Product Search with Suggestions
An online bag store implements Elasticsearch to power an instant search bar. As users type “women bags”, the app offers suggestions like “Zouk bags”, “Caprese bags”, etc., with real-time response and typo tolerance.
2. Faceted Search for Filtered Results
A fashion e-commerce site managed to use Elasticsearch to allow filters by brand, price, and category. Users can instantly update search results without full page reloading, improving UX and reducing bounce rates.
3. Geo-Based Search for Location Aware Results
A food delivery platform which is built with Laravel, uses Elasticsearch’s queries for geo-distances to show restaurants near the user’s location. When a user searches for “burger”, results are then prioritized based on proximity, cuisine, and ratings. This ensures discovery rate faster and boosts up local vendor’s visibility.
4. Personalized Content Recommendations
An e-learning platform uses Elasticsearch effectively to provide personalized course recommendations. Elasticsearch first analyzes previous searches, clicks, and enrolled topics, then powers dynamic “You may also like” sections. This real-time querying and scoring improves engagement and session time without giving heavy load to databases.
Conclusion
Laravel and Elasticsearch together play a key role in providing exact and accurate results. It also offers a robust solution for building intelligent, real-time search experiences.
Whether you are running an e-commerce store or enhancing a product catalog, this integration of Elasticsearch can significantly improve your app’s search performance and user engagement.
Frequently Asked Questions
Q: What is Laravel Elasticsearch used for?
Laravel Elasticsearch is a powerful tool that helps in implementing efficient search capabilities. It is used to integrate real-time search functionality into Laravel applications, especially useful for full-text and filtered search in e-commerce platforms.
Q: Can Elasticsearch replace SQL in Laravel?
Although Elasticsearch is a great tool for search, it is not designed to replace relational databases for transactional operations like joins or ACID compliance. It is a good tool for search and filtering, but it is not a good tool for transactional operations or for replacing SQL in the database.
Q: How do you search data in Elasticsearch from Laravel?
To search data in Elasticsearch from Laravel, we have an Elasticsearch PHP client or a service class. You can send search queries to the Elasticsearch engine and retrieve relevant documents based on conditions like match, term, or range queries.
Q: What is the difference between Scout and custom integration?
Laravel Scout helps by offering a simpler abstraction for search engines like Elasticsearch, but for complex queries, custom integration is better as it gives you more flexibility and control over indexing, queries, and performance tuning.
Author Spotlight
Kiran Saklani, Software Engineer
Related Blogs

Developing an App Like Airbnb in 2025: Costs, Key Considerations & Money-Saving Tips
Discover the cost to build an app like Airbnb, key factors, app types, and smart ways to optimize development expenses for better ROI.
Suprabhat Sen
Jun 29 ,
10 min read

Flutter vs Android Studio: What are the Key Differences?
Flutter vs Android Studio: Wondering which technology to choose to build your next app. Here is a detailed guide that will help you make a decision.
Suprabhat Sen
Jun 28 ,
14 min read

Software Development as a Service(SDaaS): The Complete Guide
Explore how SDaaS can streamline your development process, reduce costs, and boost your competitive edge. Learn how SDaaS can empower your business.
Suprabhat Sen
Jun 28 ,
16 min read