Laravel uses Laravel Searchy to implement database-level search.

Source: Internet
Author: User

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-Linham/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:

Tomlinham \ Searchy \ SearchyServiceProvider: class
By the way, register the corresponding facade in aliases of config/app. php:

'Searchy' => tomlinham \ 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 und...
$ 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 = [
'Maid \ Searchy \ Matchers \ ExactMatcher '=> 100,
'Maid \ Searchy \ Matchers \ StartOfStringMatcher '=> 50,
'Maid \ Searchy \ Matchers \ mongoingmatcher '=> 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 = [
'Maid \ Searchy \ Matchers \ ExactMatcher '=> 100,
'Maid \ Searchy \ Matchers \ StartOfStringMatcher '=> 50,
'Maid \ Searchy \ Matchers \ AcronymMatcher '=> 42,
'Tomlinheim \ Searchy \ Matchers \ ConsecutiveCharactersMatcher '=> 40,
'Maid \ Searchy \ Matchers \ StartOfWordsMatcher '=> 35,
'Maid \ Searchy \ Matchers \ StudlyCaseMatcher '=> 32,
'Maid \ Searchy \ Matchers \ mongoingmatcher '=> 30,
'Maid \ Searchy \ Matchers \ timesemediingmatcher '=> 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 = [
'Maid \ Searchy \ Matchers \ LevenshteinMatcher '=> 100
];
6. Matching
ExactMatcher

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.