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
Laravel Datatables Example
Laravel Datatables Example

In this example, I will show you how to implement/install data tables in laravel. Datatables provides users with many fu...

Read More

May-16-2020

How to Get Soft Deleted Records in Laravel 10
How to Get Soft Deleted Record...

Hey there! Ever wondered how to recover deleted data in Laravel 10 without much hassle? Well, you're in luck! I'...

Read More

Nov-27-2023

How to Install Composer on Ubuntu 22.04
How to Install Composer on Ubu...

Hey there! If you're diving into the world of PHP development on your Ubuntu 22.04 machine, you'll likely come a...

Read More

Jan-10-2024

How To Create Candlestick Chart In Laravel 9 Using Highcharts
How To Create Candlestick Char...

In this article, we will see how to create a candlestick chart in laravel 9 using highcharts. A candlestick is a ty...

Read More

Oct-06-2022