Using the Laravel searchy extension package in Laravel to implement a lightweight, database-based search feature

Source: Internet
Author: User

1. Introduction

Laravel Searchy is an easy-to-use, lightweight, MySQL-enabled Laravel search extension package that allows you to easily and efficiently implement model-based data query search capabilities, which can also be based on search engines you enable for fuzzy search and other weighting mechanisms. In addition, you do not need to install any additional software from the server to start using the expansion pack.

Note: If you are compatible with Laravel 4, refer to 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 Composer.json require entry:

"Require": {    "tom-lingham/searchy": "2.*"}

Then run composer update to download the dependency to the vendor directory.

After the installation is complete, you will need to register the service provider to config/app.php providers:

Tomlingham\searchy\searchyserviceprovider::class

By the way, register the corresponding façade in config/app.php's aliases:

' Searchy ' = Tomlingham\searchy\facades\searchy::class

3. Use

If you want to search for the name and email of the user (the corresponding data table users):

$users = searchy::users (' name ', ' email ')->query (' John Smith ')->get ();

The same functionality can be implemented as well:

$users = Searchy::search (' users ')->fields (' name ', ' email ')->query (' John Smith ')->get ();

In this example, we pass the search field through the fields method.

The above code returns an array of objects that contain search results, and if you want to return a database query object instance, you can use Getquery () instead of the Get () method:

$users = Searchy::search (' users ')->fields (' name ', ' email ')->query (' John Smith ')    ->getquery () Having (' relevance ', ' > ', ')->get ();

Querying 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 pass in a search field, you can pass parameters as an array:

$users = searchy::users ([' Name ', ' email ', ' username '])->query (' John Smith ')->get ();

Query Connection/cascading fields

Sometimes you may need to search for cascading fields, for example, if you want to return first_name and last_name at once, you can separate the fields by two colons:

$users = searchy::users (' first_name::last_name ')->query (' John Smith ')->get ();

Soft-deleted records

By default, soft-deleted records are not included in the search results. If you want to include soft-deleted records, you can do this by adding withtrashed () after the specified tables and fields:

Searchy::users (' name ')->withtrashed ()->query (' Batman ')->get ();

returns the specified field

You can specify the fields to be returned by the search:

$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 Name field relevance is returned, regardless of the input.

4. Configuration

You can publish the configuration file to the Config directory to override the default configuration:

PHP Artisan Vendor:publish

The default driver used by the search can be set in the generated profile searchy.php, which currently supports fuzzy, simple, and Levenshtein.

You can also use the following syntax to dynamically set the search driver when searching:

Searchy::d River (' Fuzzy ')->users (' name ')->query (' Batman ')->get ();

5. Search Driver

Searchy uses "Driver" to handle the match of a multi-criteria field, simply speaking, a driver is a "match" for a specified grouping to match a string based on a specified condition. Currently Searchy only supports three types of drives: simple, fuzzy and Levenshtein (experimental)

Simple

The simple search driver uses only 3 matches, each with an associated multiplication factor to best match my test environment:

protected $matchers = [    ' tomlingham\searchy\matchers\exactmatcher ' = +                 ,    ' tomlingham\searchy\ Matchers\startofstringmatcher ' +         ,    ' tomlingham\searchy\matchers\instringmatcher ' +              30,];

Fuzzy

The fuzzy search driver is another combination of the matching device. I've used the multiplication factors in this, and you can modify them manually as well:

protected $matchers = [    ' tomlingham\searchy\matchers\exactmatcher ' = +                 ,    ' tomlingham\searchy\ Matchers\startofstringmatcher '         = ' and    ' Tomlingham\searchy\matchers\acronymmatcher '               ,    ' Tomlingham\searchy\matchers\consecutivecharactersmatcher ' = +,    ' tomlingham\searchy\matchers\ Startofwordsmatcher '          = +,    ' tomlingham\searchy\matchers\studlycasematcher '            = +,    ' Tomlingham\searchy\matchers\instringmatcher '              = ' and    ' tomlingham\searchy\matchers\ Timesinstringmatcher '         = 8,];

Levenshtein

The Levenshtein search driver uses Levenshetein Distance (a string similarity algorithm) to calculate the "distance" between strings, which requires you to have one in MySQL with Levenshtein (string1, string2) A similar stored procedure (this stored procedure is in the SQL file in the extension package source res directory):

protected $matchers = [    ' tomlingham\searchy\matchers\levenshteinmatcher ' + 100];

6. Matching device

Exactmatcher

Exact match

Startofstringmatcher

Match the beginning of the string is consistent, such as "Hel" and "Hello World"

Acronymmatcher

Match string abbreviations, such as "FB" and "foo bar" or "Fred Brown"

Consecutivecharactersmatcher

Matches whether the string contains consistent contiguous characters, calculates the percentage of matching characters and applies the appropriate multiplication factor, such as searching for "FBA" matches "foo bar" or "afraid of bats" without matching "fabulous"

Startofwordsmatcher

Matches the words at the beginning of the string, for example, "Jo Ta" with "John Taylor" or "Joshua B. Takeshi"

Studlycasematcher

Hump matching, such as "HP" and "Htmlserviceprovider" or "Hashparser", but does not match "Hasprovider"

Instringmatcher

Strings contain matches, such as "SMI" and "John Smith" or "Smiley face"

Timesinstringmatcher

The string contains a number of matches, such as a search for "Tha" will match "I hope that's cat have caught that mouse" (3x) or "Thanks, it was great!" (1x)

Levenshteinmatcher

Refer to Levenshtein Driver.

7. Expansion

Drive

Implementing your own search driver is simple, just create a class that inherits from Tomlingham\searchy\searchdrivers\basesearchdriver and add an array of $matchers attributes.

Matching Device

To create your own match, you can create a class that inherits from Tomlingham\searchy\matchers\basematcher and override the Formatquery method, and then return a string formatted with the% wildcard character at the specified location. To implement more advanced extensions, you need to override the Buildquery method.

  • Related Article

    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.