The introduction

For database queries, primary keys and indexes are often powerful tools to speed up search. For text search, fuzzy search and full-text search, although MySQL’s MyISAM engine supports it, its performance often fails to meet production requirements, or the service carrying capacity is too weak.

We need a dedicated text search engine that connects to the Laravel application, which is Scout for this article.

Code time

Laravel Scout is a standalone package and library that can be easily accessed via Composer and used in conjunction with the Eloquent Model.

Algolia and Elasticsearch drivers are built into the official Scout, but the community offers other options as well. By default, Algolia would be the preferred choice.

Laravel 5.3 and above introduces the Scout library for the system:

composer require laravel/scout
Copy the code

Add the following content to the providers in the config/app.php file:

'providers' => [
    Laravel\Scout\ScoutServiceProvider::class,
]
Copy the code

If you want to use a profile to set scout, then publish the profile:

php artisan vendor:publish
Copy the code

This generates the config/scout.php file. Let’s reintroduce the Algolia SDK library file for use in the program:

composer require algolia/algoliasearch-client-php
Copy the code

To introduce the search service in the model, simply introduce it in the model file

use Laravel\Scout\Searchable;
Copy the code

Implemented manually within the model, those properties and fields can be searched by simply implementing the toSearchableArray() method. You then implement searchableAs(), which returns a string that specifies the model name for the index.

By default, Scout subscribes to create/delete/update times for the model, and data from these writes triggers index updates, index deletions, and index creation. It can be done synchronously or queued asynchronously.

Searched by index, Scout queries almost exactly the same way the Eloquent Model does. Such as:

Review::search('Lious')->get();
Copy the code

Or chain call paging:

Review::search('Lious')->paginate(20);
Copy the code

Because the search method returns a Query Builder object, you can chain-call the Query criteria and collection methods. For example, to filter a query result set:

Review::search('Lious')->where('account_id'.2)->get();
Copy the code

Whatever you do in the model, it applies here.

For some data that may not want to be indexed and put into search engines, simply declare it explicitly in the model as follows:

Review::withoutSyncingToSearch(function () {
    factory(Review::class, 10)->create();
});
Copy the code

Create 10 items of data in batches without creating indexes.

To manually control some input into the search engine and index the data, you can chain-call the searchable() method:

Review::all()->searchable();
Copy the code

Or write the data of the associated model into the search engine through the association relation:

$user->reviews()->searchable();
Copy the code

Some search data is not indexed:

Review::where('actived'.false)->unsearchable();
Copy the code

Invalid entries are not indexed to save space.

If your database is already producing a lot of data, or if you want to manually import all the definition data into the search engine during the test phase, you can do so from the command line:

php artisan scout:import App\\Review
Copy the code

Write in the last

This article introduces the methods of the Laravel Scout index class, showing how to introduce the search engine, and the method of controlling the index data in the program fine. Full text retrieval in modern Web applications to cope with the high concurrency of the scene has a set of skills, worthy of in-depth study.

Happy coding 🙂

I am @programmer assistant, continue to share programming knowledge, welcome to follow.