On an Application, Searching is one of the useful features to retrieve data quickly in real-time. Elastic search is an open resource and RESTful search engine index. ElasticSearch provides a scalable solution and performs a real-time search. It can easily integrate with Laravel.
What is Elastic Search
Elasticsearch is the distributed search and analytics engine at the heart of the Elastic Stack. Logstash and Beats facilitate collecting, aggregating, and enriching your data and storing it in Elasticsearch. Kibana enables you to interactively explore, visualize, and share insights into your data and manage and monitor the stack. Elasticsearch is where the indexing, search, and analysis magic happen.
From the official elasticsearch website.
ElasticSearch is defined as a document-oriented search-engine, It provides search, sort, filter, etc., of documents. A document appears as JSON format and it carries information that can be indexed. It actually stores the documents with represented data mapping structure and together its called an index.
ElasticSearch install on Local Environment
Install Docker and docker-compose in a local machine and now can pull from the docker repository on terminaldocker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6
After successfully pulling the image of elastic search can run the below codedocker start elasticsearch
Laravel Elasticsearch Packages
There are several packages can find on packagist. I prefer this package which is easy to install and use it. Need to run composer command to install it on your applicationcomposer require babenkoivan/scout-elasticsearch-driver
Now on config/app.php
add the following classes
'providers' =>
[
Laravel\Scout\ScoutServiceProvider::class, ScoutElastic\ScoutElasticServiceProvider::class,
]
After adding provider the elastic search classes need to publish vendor providerphp artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
php artisan vendor:publish --provider="ScoutElastic\ScoutElasticServiceProvider"
Index Configurator
Index configurator class is basically used to provides settings for an ElasticSearch index.
For creating a new index configurator can use the following artisan commandphp artisan make:index-configurator MyIndexConfigurator
After executing the command will create file MyIndexConfigurator.php
in the app
folder of your project.
You can specify the name of the index with the desire oned and setting up the index. Can set up the index with the below example
<?php
namespace App;
use ScoutElastic\IndexConfigurator;
class MyIndexConfigurator extends IndexConfigurator
{
// It's not obligatory to determine name. By default it'll be a snaked class name without `IndexConfigurator` part.
protected $name = 'my_index';
// You can specify any settings you want, for example, analyzers.
protected $settings = [
'analysis' => [
'analyzer' => [
'es_std' => [
'type' => 'standard',
'stopwords' => '_spanish_'
]
]
]
];
}
More about index settings you can find in the index management section of Elasticsearch documentation.
To create an index just run the artisan command:php artisan elastic:create-index "App\MyIndexConfigurator"
Note, that every searchable model requires its own index configurator.
Create Searchable Model
After Configure an index for a searchable model to perform search requests use the command belowphp artisan make:searchable-model MyModel --index-configurator=MyIndexConfigurator
Now, set up the mapping of your model can update the mapping with the following command php artisan elastic:update-mapping "App\MyModel"
Some Artisan command for ElasticSearch
For creating the search rule can use the commandphp artisan make:search-rule "App\MySearchRule"
To create a searchable model can use the command
php artisan make:searchable-model "App\MyIndexCofiguratorRule"
If need to drop an index configurator use that command
php artisan elastic:drop-index "App\MyIndexCofiguratorRule"
If need to drop an index configurator use that command
php artisan elastic:drop-index "App\MyIndexCofiguratorRule"
After creating a searchable model, can migrate the model with the migrate command like below
php artisan elastic:migrate
This migrate command will migrate all the searchable models as artisan migrate command
Note that all the command example is from the package as I mentioned I prefer to use. It may differ for other packages.
Hope this will helps you to implement ElasticSearch on your Laravel project. Enjoy coding! Thanks! 🙂