How to Install Elasticsearch in Laravel 10

Websolutionstuff | Dec-25-2023 | Categories : Laravel

Hey developers! If you're diving into the world of Laravel 10 and looking to supercharge your application's search capabilities, you're in the right place. In this step-by-step guide, we'll explore how to seamlessly integrate Elasticsearch into your Laravel 10 project.

Elasticsearch is a powerful, open-source search and analytics engine that makes data exploration simple and efficient. By leveraging its capabilities, you can take your Laravel applications to the next level in terms of search functionality.

In this article, we'll see how to install elasticsearch in Laravel 8, Laravel 9, and Laravel 10 and how to set up and configure elasticsearch in laravel 10.

What is Elasticsearch?

Elasticsearch is a distributed search and analytics engine built on top of Apache Lucene. It's designed to handle large volumes of data and provides lightning-fast search results, making it an ideal choice for applications where search is a critical component.

Elasticsearch is not just about searching text; it can handle structured and unstructured data, making it versatile for various use cases such as log and event data analysis, business intelligence, and full-text search.

Advantages of Elasticsearch:

  1. Speed: Elasticsearch is known for its incredible speed in searching and retrieving data. Its distributed nature allows it to parallelize operations, providing results in near real-time.

  2. Scalability: As your data grows, Elasticsearch scales horizontally, meaning you can add more nodes to your cluster to handle increased data and search loads.

  3. Full-text Search: Elasticsearch excels in full-text search, enabling users to perform complex queries, handle misspellings, and rank results based on relevance.

  4. Real-time Data: It supports real-time data indexing, making it suitable for applications that require up-to-the-moment information.

  5. Open Source: Elasticsearch is open source, meaning it's free to use and has a vast community contributing to its development and support.

Now, let's dive into the installation process for Elasticsearch in Laravel 10!

Step 1: Install Elasticsearch

Ensure that you have Java installed on your system, as Elasticsearch relies on it. You can download Java from Java's official website.

Next, let's install Elasticsearch using Composer. Open your terminal and run:

composer require elasticsearch/elasticsearch

 

Step 2: Configuration

Once the package is installed, add the Elasticsearch configuration to your Laravel application. Open your config/database.php file and add the following lines.

'elasticsearch' => [
    'driver' => 'elasticsearch',
    'hosts' => [
        [
            'host' => env('ELASTICSEARCH_HOST', 'localhost'),
            'port' => env('ELASTICSEARCH_PORT', 9200),
            'scheme' => env('ELASTICSEARCH_SCHEME', 'http'),
            'user' => env('ELASTICSEARCH_USER'),
            'pass' => env('ELASTICSEARCH_PASS'),
        ],
    ],
],

 

Step 3: Indexing Data

After configuring the Elasticsearch connection, your Laravel application is now equipped to execute search operations. By harnessing the power of the Elasticsearch PHP library, you can effortlessly integrate advanced search functionalities into your controllers or service classes.

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()
    ->setHosts(config('database.connections.elasticsearch.hosts'))
    ->build();

// Example search query
$params = [
    'index' => 'my_index',
    'body' => [
        'query' => [
            'match' => [
                'field' => 'search_keyword',
            ],
        ],
    ],
];

$response = $client->search($params);

That's it! You've successfully integrated Elasticsearch into your Laravel 10 application, enhancing its search capabilities. Feel free to explore Elasticsearch's documentation for more advanced features.

 


You might also like:

Recommended Post
Featured Post
How to Send E-mail Using Queue in Laravel 7/8
How to Send E-mail Using Queue...

Today I will show you how to send e-mail using queue in laravel 7/8, many times we can see some processes take...

Read More

Oct-30-2020

How To Avoid TokenMismatchException On Logout
How To Avoid TokenMismatchExce...

Many times we faced a Tokenmismatch error in laravel. This error occurs If you stay too long time on one form...

Read More

Jun-29-2020

How To Store Multiple Checkbox Value In Database Using Laravel
How To Store Multiple Checkbox...

In this post we will see how to store multiple checkbox value in database using laravel. Whenever you want to save multi...

Read More

May-14-2021

Laravel 8 Socialite Login with Facebook Account
Laravel 8 Socialite Login with...

In this tutorial, we will learn laravel 8 socialite login with facebook account, as you all know currently many websites...

Read More

Mar-08-2021