Scout Elasticsearch Driver In Laravel

Tram Ho

The search function is a function that any large or small project needs. And no exception, in a laravel project I’m doing using elasticsearch so today I would like to introduce to you about the package scout-elasticsearch-driver. To combine ElasticSearch with Laravel, there are many support packages such as Plastic, Elasticsearch Eloquent, elasticsearcher, … But in this article I will introduce the Scout Elasticsearch Driver package. This is a package that helps us to work with ElasticSearch and Eloquent easily. First we will go through some concepts!

1. What is Laravel Scout?

Laravel Scout is full-text search based on driver for Eloquent. In addition, it supports Algolia, Elastic Search, and because it is full-text search based on driver, anyone can create their own integration with other full-text search systems.

Laravel Scout works based on it adding search properties to existing models. Then just synchronize the data with the search service.

2. What is Elasticsearch?

  • Elasticsearch is a distributed, RESTful open source search and analysis tool built on Apache Lucene. Since its release, Elasticsearch has quickly become the most popular and widely used search engine.
  • Elasticsearch actually works like a web server, able to search quickly through RESTful protocol.

3. How to install the package

  • To install, we just need to use the composer:
  • If you use laravel version <= 5.4, you will add providers to config / app.php:
  • After successfully installing the package, use the following command line to publish config:
  • This command will automatically create the settings in config / scout.php and config / scout_elastic.php. And we can change some of its original configuration. You can refer to here So we have successfully installed the package already.

4. How to use the package in project laravel

1. Create IndexConfigurator

  • Each Eloquent Model will synchronize with an index search index, containing all searchable records for that model. In other words, you can simply understand the index in Elasticsearch as a table, but it’s not exactly the same.
  • An index configurator is used to set an index search index. To create an index configurator we use the following command: Note that the name of each index configurator will have to be different.
  • This will create a MyIndexConfigurator.php file in the app directory. Here you can name and settings for the index configurator (You can refer to other settings here )

Example: I have a simple VD setting below:

And for index configurator to work we need to run the following command:

Similarly, when you edit the settings and you want to update the index configurator, you can run the command:

Delete an index configurator:

2. Create Searchable Model

In fact, this step is mostly done in practice, we have already created corresponding models for each search index. But I still show you how to create a Searchable Model nhes ?

  • To create a Searchable Model we use the following command:
  • This will create an app / Models / MyModel.php file that looks like this:
  • Inside:
  • Mapping is the process of defining how a document and its fields are stored and indexed. Specifically, use mapping to define: type, format, .. for each model fields.
  • The function toSearchableArray () is used to customize the data to synchronize with each search index. I see a pretty good post about customizing searchable data, please refer here !
  • If we already have a model already, just use Searchable, declare the configurator, $ mapping and the function toSearchableArray ().
  • And to synchronize the data, we need to run the command:

3. Use

  • Because this is a combination package of scout and elasticsearch, after completing the above steps, we need to add an extremely important step that is to import data into each index through the following command: php artisan scout:import "AppModelsMyModel"
  • Also when CRUD a record is automatically added to the search index, after doing so we add:
  • Now through the Model you can start searching using the search () method, you can see more here !

4. Search rules

In the model you add $ searchRules property:

5. Debug

  • The package also provides two methods that can help us analyze search query results:

5. References

Share the news now

Source : Viblo