Git Product home page Git Product logo

quest's Introduction

Quest

This package enables pseudo fuzzy-searching within Laravel database and Eloquent queries. Due to its pattern matching methods, it only supports MySQL or MariaDB, though I welcome any PRs to enable support for databases like Postgres.

Much of this library is based on the fantastic work of Tom Lingham for the now abandoned Laravel Searchy package. If you're interested in the background of how the fuzzy searching works, check out the readme for that project.

Installation

Pull in the package using composer

composer require caneara/quest

Usage

Quest automatically registers a service provider containing several macros. These macros are then attached to the underlying Illuminate\Database\Query\Builder class.

Filtering results

You can perform a fuzzy-search by calling the whereFuzzy method. This method takes two parameters. The first, is the field name. The second, is the value to use for the search e.g.

DB::table('users')
  ->whereFuzzy('name', 'jd') // matches John Doe
  ->first();

User::whereFuzzy('name', 'jd') // matches John Doe
    ->first();

You can also perform a fuzzy search across multiple columns by chaining several whereFuzzy method calls:

User::whereFuzzy('name', 'jd')  // matches John Doe
    ->whereFuzzy('email', 'gm') // matches @gmail.com
    ->first();

You can also perform searches across multiple columns using orWhereFuzzy method calls:

User::whereFuzzy(function ($query) {
    $query->orWhereFuzzy('name', 'jd'); // matches John Doe
    $query->orWhereFuzzy('email', 'gm'); // matches @gmail.com
})->first();

Ordering results

When using Quest, a 'fuzzy_relevance_*' column will be included in your search results. The * is a wildcard that will be replaced with the name of the field that you are searching on e.g.

User::whereFuzzy('email', 'gm') // fuzzy_relevance_email

This column contains the score that the record received after each of the fuzzy-searching pattern matchers were applied to it. The higher the score, the more closely the record matches the search term.

Of course, you'll want to order the results so that the records with the highest score appear first. To make this easier, Quest includes an orderByFuzzy helper method that wraps the relevant orderBy clauses:

User::whereFuzzy('name', 'jd')
    ->orderByFuzzy('name')
    ->first();

// Equivalent to:

User::whereFuzzy('name', 'jd')
    ->orderBy('fuzzy_relevance_name', 'desc')
    ->first();

If you are searching across multiple fields, you can provide an array to the orderByFuzzy method:

User::whereFuzzy('name', 'jd')
    ->whereFuzzy('email', 'gm')
    ->orderByFuzzy(['name', 'email'])
    ->first();

// Equivalent to:

User::whereFuzzy('name', 'jd')
    ->orderBy('fuzzy_relevance_name', 'desc')
    ->orderBy('fuzzy_relevance_email', 'desc')
    ->first();

Applying a minimum threshold

When using Quest, an overall score will be assigned to each record within the _fuzzy_relevance_ column. This score is represented as an integer between 0 and 295.

Note that the fuzzy_relevance score is not divided by the number of columns. Therefore, it could be up to, for example, 590 if two fields match exactly.

You can enforce a minimum score to restrict the results by using the withMinimumRelevance() method. Setting a higher score will return fewer, but likely more-relevant results.

// Before
User::whereFuzzy('name', 'jd')
    ->having('_fuzzy_relevance_', '>',  70)
    ->first();

// After
User::whereFuzzy('name', 'jd')
    ->withMinimumRelevance(70)
    ->first();

When using orWhereFuzzy include the minimum relevance as an optional third parameter

// Returns results which exceed 70 on the name column or 90 on the email column
User::whereFuzzy(function ($query) {
    $query->orWhereFuzzy('name', 'jd', 70);
    $query->orWhereFuzzy('email', 'gm', 90);
})->get();

Performance (large datasets)

When searching large tables to only confirm whether matches exist, removing sorting and relevance checking will significantly increase query performance. To do this, simply supply false as a third parameter for the whereFuzzy or orWhereFuzzy methods:

DB::table('users')
  ->whereFuzzy('name', 'jd', false) 
  ->orWhereFuzzy('name', 'gm', 0, false);
  ->first();

To adjust the relevance threshold you can filter the relevance data manually if needed.

You can also further improve performance by selectively disabling one or more pattern matchers. Simply supply an array of pattern matchers you want to disable as the fourth parameter e.g.

DB::table('users')
  ->whereFuzzy('name', 'jd', true, [
    'AcronymMatcher',
    'StudlyCaseMatcher',
  ]);
  ->first();

The following pattern matchers can be included in the array:

  • ExactMatcher
  • StartOfStringMatcher
  • AcronymMatcher
  • ConsecutiveCharactersMatcher
  • StartOfWordsMatcher
  • StudlyCaseMatcher
  • InStringMatcher
  • TimesInStringMatcher

Review the /src/Matchers directory to see what each matcher does for a query.

Limitations

It is not possible to use the paginate method with Quest as the relevance fields are omitted from the secondary query that Laravel runs to get the count of the records required for LengthAwarePaginator. However, you can use the simplePaginate method without issue. In many cases this a more preferable option anyway, particularly when dealing with large datasets as the paginate method becomes slow when scrolling through large numbers of pages.

Contributing

Thank you for considering a contribution to Quest. You are welcome to submit a PR containing improvements, however if they are substantial in nature, please also be sure to include a test or tests.

License

The MIT License (MIT). Please see License File for more information.

quest's People

Contributors

brandonbest avatar dark4ce avatar mattkingshott avatar minhyme avatar nabeelyousafpasha avatar pbringetto avatar robertmarney avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

quest's Issues

Add orWhereFuzzy search option

Hi,

Was looking for a replacement for Searchy and came across your implementation.
In my use case, I need an orWhereFuzzy, so that the query returns results having at least one relevance index > 0 as opposed to having all of them > 0.

Tried contributing but need access.

I have attached the needed changes here if you are kind enough to include them.
orWhereFuzzy.zip

Thank you,
Catalin Nita.

_fuzzy_relevance_* is not limited to 100

Hello
First of all, thanks for all the job. From the documentation I understand that the relevance is between 0 and 100 ... but I have lus more than that

image

image

do you have any idea ?
thanks a lot

Matching extra characters

First of all, great package...thanks!

I'm having an issue with extra characters and slight misspellings. I wasn't sure if this was expected behavior:

Matching for guacamole...if I type:

  • guac it works
  • guacamolee does not work
  • guacemole does not work

Am I doing something wrong?

Implement or matching when chaining where Fuzzy

When searching across multiple columns, if the query would normally return a match with ONE whereFuzzy, that result is not returned if second column query returns null.

Would it be possible to built an OR function to return a better result set in that instance ?

I.e.

User::whereFuzzy('name', 'jd')
->orWhereFuzzy('email', 'gm')
->first();

Handle potentially null search fields

I'm wanting to search on two columns, one of which is nullable. At the moment I get str_replace(): Argument #3 ($subject) must be of type array|string, null given when doing so, so I suspect there isn't built-in support for this.

Is there a workaround for this?

Not able to use with Laravel 10.0

I just booted up a fresh Laravel repo and need some fuzzy search functionality. After following the docs for using, my code looks like this.

RoutingNumber::whereFuzzy('bank_name', 'WELLS')->first();

After running, I get this error:

ArgumentCountError: Too few arguments to function Illuminate\Database\Query\Expression::getValue(), 0 passed in /home/jon/Projects/Teleforce/routing_number_lookup/vendor/caneara/quest/src/Macros/WhereFuzzy.php on line 95 and exactly 1 expected in file /home/jon/Projects/Teleforce/routing_number_lookup/vendor/laravel/framework/src/Illuminate/Database/Query/Expression.php on line 34

I then tried to use the DB facade configuration, and the same error occurred.

Here are the relavant packages in my composer.json

"require": {
    "php": "^8.1",
    "caneara/quest": "^2.0",
    "guzzlehttp/guzzle": "^7.2",
    "laravel/framework": "^10.0",
    "laravel/sanctum": "^3.2",
    "laravel/tinker": "^2.8"
},
"require-dev": {
    "fakerphp/faker": "^1.9.1",
    "laravel/pint": "^1.0",
    "laravel/sail": "^1.18",
    "mockery/mockery": "^1.4.4",
    "nunomaduro/collision": "^7.0",
    "phpunit/phpunit": "^10.1",
    "spatie/laravel-ignition": "^2.0"
},

If you need more information let me know. Thanks for the time.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.