Laravel Elasticsearch: Best Integration for Lightning-fast Search

author
Mayur Upadhyay

Quick Summary

This guide details integrating Elasticsearch with Laravel to overcome slow database searches. You’ll learn to implement lightning-fast, advanced full-text search using both a direct approach and the optional Laravel Scout. The tutorial covers setup, configuration, and querying to dramatically improve search performance and user experience.

Running an eCommerce website with thousands of products? Then a slow search function can frustrate users and drive them away. Elasticsearch goes beyond the basic database queries and offers a dedicated search engine.

Integrating Elasticsearch in Laravel enables advanced features like fuzzy matching, typo tolerance, and instant filtering across massive datasets. It ensures a strategic upgrade for user retention and data discoverability.

This blog showcases a clear, actionable pathway to Laravel Elasticsearch integration. So you will have a high-performance search that scales with your needs. Let’s get straight into it.

What is Elasticsearch?

At its core, Elasticsearch is a distributed, open-source search and analytics engine built on Apache Lucene. It is designed for horizontal scalability, maximum reliability, and easy management.

Elasticsearch is widely used for:

  • Full-text search
  • Log and event data analysis
  • Business intelligence
  • Security analytics

It provides powerful text searching, fast querying, and rich analytic capabilities. So it’s suitable for high-speed indexing and querying for large-scale datasets.

Key Features of Elasticsearch

Elasticsearch’s power comes from a suite of features designed for speed, scale, and intelligence. Here are its core capabilities:

  • Full-Text Search: Go beyond simple matches. It performs complex searches across entire documents, understanding language nuances.
  • Real-Time Operation: Data becomes searchable almost instantly after being indexed, providing immediate access to new information.
  • Horizontal Scalability: Easily handle growing data by adding more servers to your Elasticsearch cluster, distributing the load seamlessly.
  • High Performance: Its distributed nature and inverted index architecture deliver blazing-fast search responses, even with massive datasets.
  • Schema-Free & JSON Documents: Store unstructured or semi-structured data flexibly without pre-defining a rigid schema.
  • Powerful Analytics: Aggregate and analyze your data to uncover trends, patterns, and summarized metrics directly from your search results.

In essence, Elasticsearch is the technology that powers the quick, relevant, and faceted search experiences. Kinda like the ones you encounter on major platforms like GitHub, Wikipedia, and Netflix.

Why Use Laravel Elasticsearch?

Integrating Elasticsearch in your Laravel application enhances the performance and improves the user satisfaction. The key benefits include:

Improved Performance & Scalability

Deliver lightning-fast search results and handle growing data volumes effortlessly, far surpassing traditional database queries.

Advanced Search Capabilities

Implement features like fuzzy matching (for typos), synonyms, and autocomplete that are complex or slow with standard SQL.

Easy Integration

A simplified API and robust Laravel packages like elasticsearch/elasticsearch and Scout drivers make setup and management straightforward.

Enhanced Developer & User Experience

Developers gain a powerful, flexible tool for building sophisticated features. It directly translates into a faster, more intuitive search experience for end-users.

The result of this integration is a high-performance application. It’ll feature instant, typo-tolerant, and highly relevant search results. They directly contribute to increased user engagement and retention.

Prerequisites for Laravel Elasticsearch Integration

To integrate Elasticsearch with Laravel, several prerequisites must be met to ensure a smooth and functional setup.

  • Composer Installed: Ensure Composer (PHP dependency manager) is available on your system to manage Laravel packages.​
  • Laravel Installed: You should have a working Laravel project, typically Laravel 8 or later for best package compatibility.​
  • Elasticsearch Server: Install and run an Elasticsearch instance. This can be done via Docker, native installation (on Windows, macOS, or Linux). Or by using a managed service such as Elastic Cloud or AWS OpenSearch.​
  • Elasticsearch PHP Client: A PHP client like elasticsearch/elasticsearch (the official low-level client) must be included in your Laravel project using Composer.​
  • Package for Laravel Integration: Optionally, install a Laravel-specific package for improved integration, though direct use of the Elasticsearch PHP client is common.​ Such as cviebrock/laravel-elasticsearch, elasticquent/elasticquent, or mailerlite/laravel-elasticsearch.
  • Configuration: Update your .env and Laravel config files (often config/services.php) with Elasticsearch host, port, and authentication details.

Meeting these prerequisites ensures your Laravel application can fully leverage Elasticsearch for search and analytics.

How to Setup Laravel Elasticsearch?

Setting up Elasticsearch with Laravel involves multiple steps, from installing Elasticsearch to integrating it into a Laravel project using PHP clients or packages. Here’s a step-by-step guide on how to set up Laravel Elasticsearch:

Step 1: Install Elasticsearch on Your Server or Local Machine

For installation on macOS, you can use Homebrew to install Elasticsearch with the command:

brew tap elastic/tap
brew install elastic/tap/elasticsearch-full
brew services start elastic/tap/elasticsearch-full

To install it on Ubuntu, use the APT package management tool:

sudo apt update
sudo apt install elasticsearch

If you prefer, you can run Elasticsearch in a Docker container to install it on your server or local machine:

docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.9.3

To install Elasticsearch on Windows, you can directly download and install it from the official Elasticsearch website. Once done, start Elasticsearch using the command. If your application handles image-heavy data, consider integrating Laravel Cloudinary to optimize image storage and retrieval alongside Elasticsearch indexing.

sudo service elasticsearch start

Step 2: Verify Elasticsearch Installation

After installing Elasticsearch, verify it is running by making a request to the local server. Here is the curl command to verify it:

curl http://localhost:9200

After firing this query, you should receive a JSON response confirming that Elasticsearch is running. The response includes the version number and cluster information.

Step 3: Install Elasticsearch PHP Client in Laravel

Laravel needs a PHP client to interact with the Elasticsearch server. The Elasticsearch PHP Client package provides the necessary tools to communicate between your Laravel application and the Elasticsearch instance. To install the client, run the following command in your Laravel project subdirectory:

composer require elasticsearch/elasticsearch

The official PHP client for Elasticsearch is installed, allowing your Laravel site to communicate with the server via the API.

Step 4: Configure Elasticsearch in Laravel

Now that Elasticsearch is installed, you need to configure the connection between Laravel and Elasticsearch. Laravel can manage the connection through the configuration files. Here is the code you need to add in config/services.php file for Elasticsearch:

'elasticsearch' => [
    'hosts' => [
        env('ELASTICSEARCH_HOST', 'localhost:9200'),
    ],
],

Then, in your .env file, define the Elasticsearch host:

ELASTICSEARCH_HOST=localhost:9200

Laravel is now configured to communicate with your local Elasticsearch instance using the host configuration. If you are working in production, update the .env file with your production Elasticsearch host.

Step 5: Create an Elasticsearch Service Class

To interact with Elasticsearch, it’s best to encapsulate the logic into a service class. This service will handle indexing and querying data in Elasticsearch. Here is how to create an ElasticsearchService.php class in your App\Services directory:

use Elasticsearch\ClientBuilder;
class ElasticsearchService
{
    protected $client;
    public function __construct()
    {
        $this->client = ClientBuilder::create()
            ->setHosts([config('services.elasticsearch.hosts')])
            ->build();
    }
    public function search($index, $query)
    {
        $params = [
            'index' => $index,
            'body' => [
                'query' => [
                    'match' => $query
                ]
            ]
        ];
        return $this->client->search($params);
    }
}

This service class allows your Laravel application to search for data by communicating with the Elasticsearch instance. It can be expanded with methods for indexing, updating, and deleting documents.

Step 6: Index Data Automatically with Model Observers

To keep the Elasticsearch index up to date with your Laravel database, you can use Model Observers to automatically index, update, or delete records. For projects that handle non-relational datasets, exploring Laravel with MongoDB can further enhance data synchronization and scalability.

To create an observer for the model you want to index in Elasticsearch, e.g., ArticleObserver.php:

use App\Services\ElasticsearchService;
class ArticleObserver
{
    protected $elasticsearch;
    public function __construct(ElasticsearchService $elasticsearch)
    {
        $this->elasticsearch = $elasticsearch;
    }
    public function created(Article $article)
    {
        $this->elasticsearch->index([
            'index' => 'articles',
            'id'    => $article->id,
            'body'  => $article->toArray(),
        ]);
    }
    public function updated(Article $article)
    {
        $this->elasticsearch->index([
            'index' => 'articles',
            'id'    => $article->id,
            'body'  => $article->toArray(),
        ]);
    }
    public function deleted(Article $article)
    {
        $this->elasticsearch->delete([
            'index' => 'articles',
            'id'    => $article->id,
        ]);
    }
}

Attach this observer to the model in AppServiceProvider.php:

Article::observe(ArticleObserver::class);

Now, when records are created, updated, or deleted in your Laravel database, the changes are automatically reflected in Elasticsearch. That will keep your data synchronized.

Step 7: Search Data from Elasticsearch

Now that your data is indexed, you can perform searches. Elasticsearch provides various types of search queries, including full-text searches, match queries, and more. To search for an article, use the ElasticsearchService class:

$results = app(ElasticsearchService::class)->search('articles', ['title' => 'Some title']);

The search method sends a query to Elasticsearch and retrieves the relevant results. You can display or process these results as needed.

Step 8: Test Elasticsearch Setup

Before deploying Elasticsearch, you should test the integration locally to ensure everything works as expected. You can run search queries and check if Elasticsearch returns the correct results. For example:

curl -X GET "localhost:9200/articles/_search?q=title:Some%20title"

If the results are returned based on the search term you provided, your Elasticsearch setup will successfully process search queries. Test a variety of queries and operations like indexing, updating, and deleting to ensure your system works correctly.

Using Laravel Scout With Elasticsearch (Optional Approach)

Laravel Scout provides an elegant, driver-based solution for adding full-text search to your Eloquent models. When you pair Scout with an Elasticsearch driver, it abstracts away much of the complexity. Scout offers a standardized way to manage search operations.

When to Use Laravel Scout With Elasticsearch?

This approach will be helpful with:

  • Rapid Development: Ideal when your primary goal is to add powerful search to Eloquent models with minimal boilerplate code.
  • Standard Use Cases: Perfect for common requirements like model-based indexing, simple filtering, and basic full-text search.
  • Leveraging Laravel Conventions: Best if you want to use Laravel’s built-in methods like Model::search(‘query’) and maintain a consistent, framework-centric workflow.

How to Use Laravel Scout With Elasticsearch?

Step 1: Installation: Install Scout and an Elasticsearch driver package (e.g., laravel/scout and matchish/laravel-scout-elasticsearch).

Step 2: Configuration: Add your Elasticsearch credentials to your .env file and specify the driver in your config/scout.php configuration.

Step 3: Model Setup: Add the Laravel\Scout\Searchable trait to the Eloquent models you wish to make searchable.

Step 4: Indexing: Scout will automatically handle indexing new, updated, or deleted models. It keeps your search data in sync with your database.

Step 5: Searching: Use the simple Scout syntax to perform searches: Product::search(‘wireless keyboard’)->get();.To leverage it effectively with all benefits, consult with a Laravel development company and make your site’s search more responsive.

Want to build a robust and well-designed Laravel site?

Basic Operations with Laravel Elasticsearch Package

When using the Laravel Elasticsearch package (such as the MailerLite Laravel Elasticsearch package), developers can perform a variety of basic operations. It includes indexing, searching, updating, and deleting documents in an Elasticsearch index. Here’s a breakdown of the essential operations using Laravel and Elasticsearch:

Indexing Data

Indexing involves adding new documents to your Elasticsearch index. This is similar to inserting rows into a database table in SQL.

use Elasticsearch\ClientBuilder;
class ElasticsearchService
{
    protected $client;
    public function __construct()
    {
        $this->client = ClientBuilder::create()->build();
    }
    public function indexData($index, $id, $body)
    {
        $params = [
            'index' => $index,
            'id'    => $id,
            'body'  => $body,
        ];
        return $this->client->index($params);
    }
}

Then you can call this method to index data:

$elasticsearchService->indexData('articles', 1, ['title' => 'Introduction to Elasticsearch']);

This will index an article with ID 1 and the title “Introduction to Elasticsearch.”

Search Data

To search data in Elasticsearch, you can use the match query to find documents based on specific fields.

public function searchData($index, $query)
{
    $params = [
        'index' => $index,
        'body' => [
            'query' => [
                'match' => $query,
            ],
        ],
    ];
    return $this->client->search($params);
}

Here is how you can use it:

$results = $elasticsearchService->searchData('articles', ['title' => 'Elasticsearch']);

The above query performs a search in the articles index for articles with titles matching “Elasticsearch”.

Update Documents

You can update existing documents in Elasticsearch using the update API. The document ID is required for this operation.

public function updateData($index, $id, $body)
{
    $params = [
        'index' => $index,
        'id'    => $id,
        'body'  => [
            'doc' => $body,
        ],
    ];
    return $this->client->update($params);
}

To update a document:

$elasticsearchService->updateData('articles', 1, ['title' => 'Updated Elasticsearch Introduction']);

This will update the title of the article with ID 1.

Delete Documents

Deleting a document from an index is straightforward. You need to provide the index name and the document ID.

public function deleteData($index, $id)
{
    $params = [
        'index' => $index,
        'id'    => $id,
    ];
    return $this->client->delete($params);
}
To delete a document:
$elasticsearchService->deleteData('articles', 1);

The above query will delete the document with ID 1 from the articles index.

Bulk Operations

Elasticsearch also supports bulk operations, allowing you to perform multiple actions like indexing, updating, or deleting documents in one request.

public function bulkOperations($operations)
{
    $params = ['body' => $operations];
    return $this->client->bulk($params);
}
Here is the example usage:
$operations = [
    ['index' => ['_index' => 'articles', '_id' => 1]],
    ['title' => 'Bulk Insert Article 1'],
    ['index' => ['_index' => 'articles', '_id' => 2]],
    ['title' => 'Bulk Insert Article 2'],
];
$elasticsearchService->bulkOperations($operations);

This will bulk index two articles in the articles index.

These basic operations provide the foundation for interacting with Elasticsearch in a Laravel application. By using methods like index, search, update, and delete, experts can build robust search functionalities.

Real-World Use Cases of Laravel Elasticsearch

Laravel Elasticsearch is commonly used in real-world applications to enhance search functionality. Below are several real-world use cases of using Elasticsearch in Laravel-based website:

eCommerce Search Optimization

In an eCommerce platform, Elasticsearch is used to provide fast, accurate, and scalable product searches. Features like full-text search, faceted search, filtering, and real-time auto-suggestions can improve user experience.

Example Use Case:

  • Product Catalog Search: Elasticsearch indexes products with fields like name, description, tags, category, and price. When a user types a query, Elasticsearch delivers results with filters for price ranges, categories, and other metadata.
  • Autocomplete: It enables autocomplete suggestions for search terms while users are typing, improving user experience.

Blog or News Websites for Full-Text Search

News or blog websites with large article libraries use Elasticsearch to allow users to search content quickly and accurately. Elasticsearch’s full-text search capabilities allow for more relevant search results by also matching for semantic relationships within the text.

Example Use Case:

  • Article Search: Blog and media websites index all content (titles, bodies, tags, and metadata) in Elasticsearch. Users can search articles based on keywords, and Elasticsearch can deliver results ranked by relevance.
  • Search Filtering: Elasticsearch can filter articles by date, category, author, or tags to refine results.

Social Media or Forum Platforms

On platforms like social media or forums, where user-generated content grows rapidly, Elasticsearch is critical. It is used to enhance the platform’s ability to find posts, group discussions, or related topics. That makes large-scale queries manageable.

Example Use Case:

  • Search Posts and Comments: Index posts and comments in real-time using Laravel observers. Elasticsearch can then search through the entire content pool for relevant keywords.
  • User Search: Users can be indexed to support features like finding other members, follower counts, or user-generated content within the platform.

Geospatial Search for Location-Based Applications

Elasticsearch supports geospatial queries, making it useful for applications where location-based searches are crucial. This is especially beneficial for platforms that offer location-based services like restaurant finders, hotel bookings, and event listings.

Example Use Case:

  • Local Business Search: Elasticsearch can index business data along with latitude and longitude. Users can then search for businesses within a specific distance or location (e.g., “restaurants near me”).
  • Event Search by Location: Elasticsearch allows filtering of events by location, date, and type, delivering location-based results quickly.

Real-Time Analytics and Monitoring Applications

Elasticsearch is used to store and search logs or analytical data in real-time. Sites with large-scale logging can push their log data to Elasticsearch and retrieve it efficiently for monitoring.

Example Use Case:

  • Application Logging: Laravel applications can send logs to Elasticsearch, which makes it easier to search, filter, and analyze logs. Meanwhile, Browsershot Laravel can assist in generating visual snapshots or performance reports from such analytics dashboards.
  • Real-Time Data Monitoring: Elasticsearch can be used in dashboards to monitor website traffic, server performance, and user interactions. Intelligent monitoring systems using Laravel OpenAI can even automate insights and anomaly detection from Elasticsearch datasets.

These use cases show us the importance of Elasticsearch in real-world web applications.

Final Thoughts

Laravel Elasticsearch provides a powerful and flexible solution for implementing efficient search capabilities. By leveraging the robust features of Elasticsearch, you can enhance user experience and improve data accessibility.

Implementing Laravel Elasticsearch starts with installing it on a local machine or server and ends with finally testing if it’s working. Once you are done with the setup, you can perform operations like index, search, update, and delete. These operations can be implemented in real-world scenarios for eCommerce search optimization, real-time analytics, and more.

If you want to enhance your site’s search functionality and enhance user experience with such tools, hire Laravel developers with us today!

FAQs About Laravel Elasticsearch

What is Elasticsearch best used for?

Elasticsearch is best used for applications that require fast and efficient full-text search and real-time analytics. It’s particularly well-suited for use cases such as eCommerce, content management systems, social media, and analytics.

Can Elasticsearch replace SQL?

No, Elasticsearch cannot fully replace SQL databases. While it’s excellent for search and analytics, SQL databases are still essential for structured data storage, relational operations, and transactions. Elasticsearch can complement SQL databases by providing efficient search and analytics capabilities.

What does Elasticsearch provide?

Elasticsearch provides a distributed search and analytics engine that can handle large volumes of data and provide fast and relevant search results. It offers features such as full-text search, real-time analytics, geolocation search, and aggregation capabilities.

How do I reindex data in Laravel Elasticsearch?

If you’re using Laravel Scout, you can use the Artisan command php artisan scout:import “App\Models\YourModel”. For a direct integration, you would typically create a new index, adjust mappings. Then use the Elasticsearch client’s bulk API to repopulate all records from your database.

Is Laravel Scout necessary for Elasticsearch integration?

No, Scout is not necessary. You can use the official Elasticsearch PHP client for a direct, low-level integration. However, Scout is highly recommended as it dramatically simplifies the process. It provides a standardized and elegant API for common search operations within the Laravel framework.

author
Leading digital transformation projects with precision and creativity, Mayur turns complex ideas into results-driven web solutions. As Project Leader at WPWeb Infotech, he leverages his expertise in Laravel, PHP, Shopify, and Drupal to deliver innovation, scalability, and long-term success.

Supercharge Search with Laravel & Elasticsearch

Deliver blazing-fast, accurate search results. Our Laravel experts can help you integrate and optimize Elasticsearch seamlessly.