This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Easy Ways to Configure Elasticsearch in Laravel

Updated on June 25, 2025

4 Min Read

Elasticsearch is an amazing helper if you are developing a Laravel application having large datasets like user profiles, articles and items, enabling users search and discover results in milliseconds. It is extremely accurate, fast, and scales so well for full text searches.

As to why one would need this guide, it’s probably because starting off integrating Elasticsearch with Laravel can be challenging. I will make sure that all kinds of readers right from a beginner facing a challenge making things work, or an advanced developer willing to optimize it further will have everything explained step by step through the whole process. So let’s get started!

What is Elasticsearch & Why Use it With Laravel?

Elasticsearch is a potent Lucene-based search engine. It is made for full-text search, autocomplete, filtering, real-time search, and even analytics on massive amounts of data.

Now, why combine it with Laravel?

  • Laravel is a robust PHP framework for modern web apps.
  • With tools like Laravel Scout, it becomes easy to index and search models using Elasticsearch.
  • Elasticsearch significantly outperforms SQL-based LIKE searches, especially on large datasets.

So if you want lightning-fast search performance, fuzzy matches, or advanced filtering, Elasticsearch is the way to go.

Pre-Requisites Before You Begin

Before jumping into code, make sure you have the following:

For Cloudways users:

  • You can enable Elasticsearch in just a few clicks under Server > Settings & Packages > Packages > Elasticsearch. No manual install needed.

Installing Elasticsearch on Your System (Local & Cloudways)

Here’s how you can install Elastic Search on your system.

For Local (Mac/Linux/Windows):

If you’re working locally, the simplest way is via Docker:

docker run -d -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.17.0

Once it’s running, check if Elasticsearch is working:

curl http://localhost:9200

You should see a JSON response like:

{

"name": "docker-node",

"cluster_name": "docker-cluster",

...

}

How to Install ElasticSearch on Cloudways Platform?

  • Log in to your Cloudways Platform
  • Go to your server
  • Click on Settings & Packages
  • Under the Packages tab, enable Elasticsearch
  • Wait for it to finish installation

Cloudways Packages

Done! No server-level configurations needed.

Installing Laravel Scout: The Laravel Search Package

Laravel doesn’t come with Elasticsearch support out of the box. That’s where Laravel Scout comes in. To install Scout, run:

composer require laravel/scout

Then publish the configuration file:

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

This creates config/scout.php.

Choosing the Right Elasticsearch Driver

Laravel Scout needs a driver to connect to Elasticsearch. The most popular community-supported package is:

composer require babenkoivan/scout-elasticsearch-driver

Now why do we use this package? Here’s why:

  • It’s highly customizable
  • Actively maintained
  • Compatible with Laravel Scout

Once installed, update config/scout.php:

'driver' => 'elasticsearch',

Then add a new config file for Elasticsearch:

php artisan vendor:publish --tag=scout-elasticsearch-driver-config

This creates: config/elasticsearch.php. Update the file with your connection details:

'hosts' => [

env('ELASTICSEARCH_HOST', 'localhost:9200'),

],

In .env, add:

SCOUT_DRIVER=elasticsearch

ELASTICSEARCH_HOST=127.0.0.1:9200

Connecting Laravel to Elasticsearch (Step-by-Step)

At this point, Scout is installed and Elasticsearch is running. Let’s link it all together.

Step 1: Make Your Model Searchable

Open your model (e.g. Product.php) and add:

use Laravel\Scout\Searchable;

class Product extends Model

{

use Searchable;

}

Step 2: Customize Data to be Indexed (optional)

Inside the same model, add:

public function toSearchableArray()

{

return [

'id' => $this->id,

'title' => $this->title,

'description' => $this->description,

];

}

This allows you to control exactly what gets indexed.

Step 3: Import Data

You will have to execute the following command:

php artisan scout:import "App\Models\Product"

That’s it! Your data is now searchable in Elasticsearch.

Creating Indexes and Searchable Models in Laravel

You don’t need to manually create indexes in Elasticsearch, the driver does it for you when you call scout:import. But if you want to create advanced mappings, here’s how:

Creating Custom Mapping

You can add the following code block inside your model:

public function searchableAs()

{

return 'products_index';

}

And in config/elasticsearch.php, you can define index settings. Also, use:

Product::search('laptop')->get();

To search within the index.

Common Troubleshooting Tips

1. Laravel can’t connect to Elasticsearch

  • Check if Elasticsearch is running:
curl http://localhost:9200
  • For Cloudways Users, confirm the internal IP and port

2. Nothing is being indexed

  • Make sure toSearchableArray() returns valid data
  • Try re-importing:
php artisan scout:flush "App\Models\Product"

Then:

php artisan scout:import "App\Models\Product"

3. Search returns empty results

  • Confirm the field is in the index.
  • Use Product::search(‘keyword’)->get(); for debugging

Why Does Cloudways Make Setting Up ElasticSearch Easy?

On Cloudways, you don’t need to set up Elasticsearch manually. It’s literally just a toggle switch, and it’s optimized out of the box. Here’s what Cloudways adds:

  • 1-Click Elasticsearch Installation
  • Managed Backups in case you want to roll back.
  • Staging Environment to test search features safely.
  • Built-in Performance Monitoring for your Laravel app.
  • Plus, their support is available 24/7 if anything goes wrong.

Conclusion

Although it might appear complicated at first, setting up Elasticsearch in Laravel is actually an easy one once you break it down. You install Laravel Scout, use a driver to link it to Elasticsearch, and enable search functionality for your models. That’s all it takes to give your users blazing fast search capabilities.

Whether you’re running a blog, e-commerce platform, or any dynamic app, integrating Elasticsearch will drastically improve your performance — and with Cloudways, you can get it done in just a few clicks. So why wait?

Frequently Asked Questions

Q1. Can I use Elasticsearch with Laravel without Scout?

Yes, but Scout simplifies everything and is highly recommended.

Q2. What version of Elasticsearch should I use?

Use 7.x (like 7.17) for best compatibility with most Laravel packages.

Q3. Is Elasticsearch available on Cloudways?

Yes, it can be enabled from the server panel with a few clicks.

Q4. Can I customize the fields indexed in Elasticsearch?

Absolutely! Use toSearchableArray() in your model to control what gets indexed.

Q5. What happens if Elasticsearch goes down?

Scout won’t break your app, it will just return no results. But for production, use monitoring or fallback strategies.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Salwa Mujtaba

Salwa Mujtaba is a Technical Content Writer at Cloudways. With a strong background in Computer Science and prior experience as a team lead in Cloudways Operations, she brings a deep understanding of the Cloudways Platform to her writing. Salwa creates content that simplifies complex concepts, making them accessible and engaging for readers. When she's not writing, you can find her enjoying good music, reading a book, or spending quality time with her family.

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour