Use the LaravelSearchy extension package in Laravel to implement database-based lightweight search.
1. Introduction
Laravel Searchy is an easy-to-use, lightweight Laravel search extension package that supports MySQL. With this extension package, you can easily and efficiently implement model-based data query and search functions, the extension package can also implement fuzzy search and other weighting mechanisms based on the search engine you have enabled. In addition, you can use the extension package without installing any other software on the server.
Note: If you want to be compatible with Laravel 4, refer to the https://github.com/TomLingham/Laravel-Searchy/tree/1.0
2. Installation
We use Composer to install the extension package, and add the following two dependencies to The require entry of composer. json:
"require": { "tom-lingham/searchy" : "2.*"}
Then run composer update to download the dependency to the vendor directory.
After the installation is complete, register the service provider to the providers of config/app. php:
TomLingham\Searchy\SearchyServiceProvider::class
By the way, register the corresponding facade in aliases of config/app. php:
'Searchy' => TomLingham\Searchy\Facades\Searchy::class
3. use
If you want to search for the user (corresponding to the users table) name and Email:
$users = Searchy::users('name', 'email')->query('John Smith')->get();
The same function can also be implemented as follows:
$users = Searchy::search('users')->fields('name', 'email')->query('John Smith')->get();
In this example, we use the fields method to pass the search field.
The above code returns an array of objects containing search results. to return an instance of a database query object, you can use getQuery () instead of the get () method:
$users = Searchy::search('users')->fields('name', 'email')->query('John Smith') ->getQuery()->having('relevance', '>', 20)->get();
Query multiple fields
For example, if you want to search for a user's name, Email, and user name, you can call:
$users = Searchy::users('name', 'email', 'username')->query('John Smith')->get();
If you need to dynamically input search fields, you can pass parameters in the form of arrays:
$users = Searchy::users(['name', 'email', 'username'])->query('John Smith')->get();
Query connection/Cascade fields
Sometimes you may need to search for cascading fields. for example, if you want to return first_name and last_name at one time, you can use two colons to separate fields:
$users = Searchy::users('first_name::last_name')->query('John Smith')->get();
Soft delete record
By default, a soft delete record does not contain search results. If you want to include soft deletion records, you can add withTrashed () to the specified table and field:
Searchy::users('name')->withTrashed()->query('Batman')->get();
Returns the specified field.
You can specify the fields to be returned:
$users = Searchy::users('first_name::last_name')->query('John Smith')->select('first_name')->get();// Or you can swap those around...$users = Searchy::users('first_name::last_name')->select('first_name')->query('John Smith')->get();
In addition, the alias field relevance is returned, regardless of the input.
4. configuration
You can publish the configuration file to the config directory to overwrite the default configuration:
php artisan vendor:publish
You can set the default search driver in the generated configuration file searchy. php. Currently, fuzzy, simple, and levenshtein are supported.
You can also use the following syntax to dynamically set the search driver during search:
Searchy::driver('fuzzy')->users('name')->query('Batman')->get();
5. search-driven
Searchy uses the "driver" to process multi-condition field matching. Simply put, the driver is the "separator" of a specified group, which is used to match strings based on specified conditions. Currently, Searchy only supports three types of drivers: Simple, Fuzzy, and Levenshtein (experimental)
Simple
The Simple search driver only uses three replicas, each of which has a relevant multiplication factor to better match my test environment:
protected $matchers = [ 'TomLingham\Searchy\Matchers\ExactMatcher' => 100, 'TomLingham\Searchy\Matchers\StartOfStringMatcher' => 50, 'TomLingham\Searchy\Matchers\InStringMatcher' => 30,];
Fuzzy
The Fuzzy search driver is another matching component. Here, the multiplication factors are all used by me. you can also manually modify them:
protected $matchers = [ 'TomLingham\Searchy\Matchers\ExactMatcher' => 100, 'TomLingham\Searchy\Matchers\StartOfStringMatcher' => 50, 'TomLingham\Searchy\Matchers\AcronymMatcher' => 42, 'TomLingham\Searchy\Matchers\ConsecutiveCharactersMatcher' => 40, 'TomLingham\Searchy\Matchers\StartOfWordsMatcher' => 35, 'TomLingham\Searchy\Matchers\StudlyCaseMatcher' => 32, 'TomLingham\Searchy\Matchers\InStringMatcher' => 30, 'TomLingham\Searchy\Matchers\TimesInStringMatcher' => 8,];
Levenshtein
Levenshtein search driver uses Levenshetein Distance (a string similarity algorithm) to calculate the "Distance" between strings. This requires that you have a levenshtein (string1, string2) similar stored procedures (this stored procedure is in the SQL file under the res Directory of the extended package source code ):
protected $matchers = [ 'TomLingham\Searchy\Matchers\LevenshteinMatcher' => 100];
6. matchingExactMatcher
Exact match
StartOfStringMatcher
Whether the matching string starts with the same, such as "El" and "Hello World"
AcronymMatcher
Matching string abbreviations, such as "fb" and "foo bar" or "Fred Brown"
ConsecutiveCharactersMatcher
Whether the matching string contains consistent continuous characters. In addition, the percentage of matching characters is calculated and the corresponding multiplication factor is applied, for example, "fba" matches "foo bar" or "Afraid of bats" instead of "fabulous"
StartOfWordsMatcher
Match the words starting with a string, for example, "jo ta" and "John Taylor" or "Joshua B. Takeshi"
StudlyCaseMatcher
Hump matching, such as "hp" and "HtmlServiceProvider" or "HashParser", but does not match "hasProvider"
InStringMatcher
String inclusion matching, such as "smi" and "John Smith" or "Smiley Face"
TimesInStringMatcher
String contain times match. for example, if you search for "tha", it will match "I hope that cat has caught that mouse" (3 ×) or "Thanks, it was great !" (1 ×)
LevenshteinMatcher
Refer to Levenshtein Driver.
7. expansion
Driver
It is easy to implement your own search driver. you only need to create a class inherited from tomlinham \ Searchy \ SearchDrivers \ BaseSearchDriver and add a $ matchers attribute array.
Matching
To create your own Matchers, you can create a class inherited from tomlinham \ Searchy \ Matchers \ BaseMatcher, override the formatQuery method, and return a string formatted with the % wildcard at the specified position. To implement more advanced extensions, you need to override the buildQuery method.