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-demo

  2. Then, run this command to publish the docker-compose.yml file:

    php artisan sail:install --with=mysql

    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 -d

    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

    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

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:9200

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

  2. Then, bring up the containers:

    ./vendor/bin/sail up -d

  3. Next, we have to run migrations and seed data:

    ./vendor/bin/sail artisan migrate
    ./vendor/bin/sail artisan db:seed

  4. Finally, access the API at:

    http://localhost/api/search?query=test

[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 *

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

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF