Using Elasticsearch with Laravel
In this article, we will provide a complete guide for using Elasticsearch with Laravel. Elasticsearch is one of the best solutions for handling search in our applications.
On the other hand, Kibana can visualize our data and provide us with the ability to share it.
Assuming that we already have a Laravel application (Dockerize Laravel, Nginx, MariaDB, PhpMyAdmin, Redis and Npm) and we decide to add Elasticsearch.
Whether or not your application has been Dockerized, our approach will involve using Elasticsearch and Kibana through Docker.
Let’s go to our docker-compose.yml
file. (If there is no docker-compose.yml
file you should create it on root directory of your project)
In our Docker Compose file, we need to create two containers as shown below:
After that just need to run below command inside our terminal: (If your containers are already running, we will run docker compose down
command first)
docker compose up -d
This command will create the containers we need and start them running.
The environment
section sets some configuration parameters for Elasticsearch. discovery.type=single-node
sets the Elasticsearch node as a single node cluster. bootstrap.memory_lock=true
locks the memory so that it cannot be swapped to disk. ES_JAVA_OPTS
sets the Java heap size to 512MB.
The ulimits
section sets the memlock
limit to -1
to allow Elasticsearch to lock the memory.
The ports
option maps the Elasticsearch HTTP port, 9200, to the same port on the Docker host. The volumes
section creates a named volume laravelelastic
to store the Elasticsearch data files.
NOTE:
FORWARD_ELASTIC_PORT
option will be used when we want to define a custom port for our services or if you installed any of services locally alongside docker and there is port-interference.
So when we run the below command:
docker ps
We should see something like this:
We have our services and containers now and we should go to the our Laravel application and configure Elasticsearch within our web-app.
Laravel has a very nice driver for any search engines that is called Laravel Scout. Scout bring us full-text search ability inside our app.
At the time of writing this article, it won’t support Elasticsearch officially but don’t worry, with the help of another package called Explorer, we should be good to go for using Elasticsearch.
Let’s install Laravel Scout:
composer require laravel/scout
And publish scout configuration to add Elasticsearch settings:
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
That’s it. Now we will install the Jeroen-G/Explorer package (Elasticsearch driver for Laravel Scout):
composer require jeroen-g/explorer
We will need the configuration file to define our indexes:
php artisan vendor:publish --tag=explorer.config
Now we can start to configure and add Elasticsearch to our app but for having a little break, we can see Elasticsearch active node by going to: http://localhost:9200
And http:localhost:5601 is Kibana default dashboard address for checking:
We should decide to use Elastic on which model of our app. Let’s imagine we have an Article model:
We want to have search on that. First we will add use Searchable;
trait from Laravel\Scout\Searchable;
.
This trait give us access to some useful methods. Add toSearchableArray
method:
This method determines which fields of our model should be searchable. If we don’t customize it, by default, the entire form of model will be persisted to it’s search index.
We may define a makeAllSearchableUsing
method on the Article model. This method helps us to add any eager relationship loading that could be necessary before importing our model to Elasticsearch:
The last method we need to add is mappableAs
. If we don’t write this one, the Elasticsearch will try to recognize each field type by itself but writing this method causes a more efficient and better configuration. Be careful about implementing the Explored interface!
We are done with Article
model and next we will open ArticleController
and use Elasticsearch on index()
method:
I know it’s quite a long story, but bear with me:) because the result is something awesome
We have made our model and controller ready, and there are just a few tiny configurations left. Let’s open explorer.php
in the config folder and add these lines to the connection section:
Defining a custom username and password makes everything more secure.
There are also other connection methods, such as cloud configuration, which you read more about them here. After that, in the same file, we will correct the indexes array with this:
Basically, we index our model in Elasticsearch. Finally, add these two lines to the .env
file:
Run the command below to instruct Laravel Scout to add records to the Elasticsearch engine, and our Elasticsearch is ready to go!
php artisan scout:import App\\Models\\Article
Now, we have Elasticsearch integrated with Laravel that is ready to use. However, Elasticsearch is a vast topic that includes many areas such as types, analyzers, queue-based indexing, and more. If you are interested in learning more about this, let me know, and I can write another article about it!
READ MORE: