Bobcares

Laravel Sail Elasticsearch | How to Integrate

by | Jan 12, 2024

Learn how to integrate Laravel Sail & Elasticsearch. Our Laravel Support team is here to help you with your questions and concerns.

Laravel Sail & Elasticsearch

If you are looking for a guide to help you seamlessly integrate Elasticsearch into a Laravel application via Laravel Sail, and Docker, you are in the right place.

  1. We begin by creating a new Laravel application named ‘elasticsearch-2021-demo’ with the Laravel Installer:
    laravel new elasticsearch-2021-demoCopy Code
  2. Then, run this command to publish the docker-compose.yml file:
    php artisan sail:install --with=mysqlCopy Code

    Our experts would like to point out that Laravel Sail comes pre-installed in fresh Laravel 8 applications, and running ‘sail:install’ is all it takes to publish the docker-compose.yml file.

  3. Now, it is time to bring up the containers using the following command:
    sail up -dCopy Code

    Additionally, if we add ‘./vendor/bin/’ to our `$PATH`, we can use ‘sail’ directly; else, we have to use ‘./vendor/bin/sail up -d’.

  4. Now, we can access our application at `http://localhost` and the MySQL container at `127.0.0.1:3306`. In case we run into port conflicts, we can tweak the local port settings in the ‘.env’ file as seen here:
    
    FORWARD_DB_PORT=33060
    APP_PORT=8000
    Copy Code

    Now, run ‘sail up -d’ again to apply the changes, and our Laravel app will now be accessible at `http://localhost:8000`.

How to integrate Elasticsearch with Laravel Sail

Now, to use Elasticsearch with our Sail setup, we have to add a new service to the ‘docker-compose.yml’ file. Add the following snippet:

How to integrate Elasticsearch with Laravel Sail

Additionally, we have to create a new volume for this service called ‘sailelasticsearch’:

volumes:
sailmysql:
driver: local
sailelasticsearch:
driver: local
Copy Code

Now, run ‘sail up -d’ again, and watch as the official Elasticsearch image is downloaded, and the container boots up at port 9200 locally.

Verify Elasticsearch Installation

We can verify Elasticsearch is up and running by executing the following curl command:

curl localhost:9200Copy Code

Then, we will see an output with details like cluster name, version, and a tagline that reads, “You Know, for Search.”

Furthermore, Use the container name, not localhost, to visit another container within another container.

To access Elasticsearch while within Laravel, we should use “curl es:9200”.

Alternative method

If you are looking for a RESTful API for Elasticsearch within Laravel Sail, there’s an alternative method.

  1. To begin with, clone the repository
    git clone git@github.com:kaanosman/laravel-sail-elasticsearch
    cd laravel-sail-elasticsearch
    Copy Code
  2. Then, bring up the containers:
    ./vendor/bin/sail up -dCopy Code
  3. Next, we have to run migrations and seed data:
    ./vendor/bin/sail artisan migrate
    ./vendor/bin/sail artisan db:seed
    Copy Code
  4. Finally, access the API at:
    http://localhost/api/search?query=testCopy Code

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

In brief, our Support Experts demonstrated how to integrate Laravel Sail & Elasticsearch.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Speed issues driving customers away?
We’ve got your back!