Coder Social home page Coder Social logo

fuse's Introduction

The Fuse logo, a violet asterisk, in reference to the Fuse.js logo

Fuse

A fuzzy search library for PHP

Tests Packagist PHP Version

This is a PHP port of the awesome Fuse.js project and aims to provide full API compatibility wherever possible.

Check out their demo and examples to get a good feel for what this library is capable of.

Latest compatible Fuse.js version: 7.0.0


Table of Contents:


Installation

This package is available via Composer. To add it to your project, just run:

composer require loilo/fuse

Note that at least PHP 7.4 is needed to use Fuse.

Usage

Here's a simple usage example:

<?php
require_once 'vendor/autoload.php';

$list = [
    [
        'title' => "Old Man's War",
        'author' => 'John Scalzi',
    ],
    [
        'title' => 'The Lock Artist',
        'author' => 'Steve Hamilton',
    ],
    [
        'title' => 'HTML5',
        'author' => 'Remy Sharp',
    ],
    [
        'title' => 'Right Ho Jeeves',
        'author' => 'P.D Woodhouse',
    ],
];

$options = [
    'keys' => ['title', 'author'],
];

$fuse = new \Fuse\Fuse($list, $options);

$fuse->search('hamil');

This leads to the following results (where each result's item refers to the matched entry itself and refIndex provides the item's position in the original $list):

[
    [
        'item' => [
            'title' => 'The Lock Artist',
            'author' => 'Steve Hamilton',
        ],
        'refIndex' => 1,
    ],
    [
        'item' => [
            'title' => 'HTML5',
            'author' => 'Remy Sharp',
        ],
        'refIndex' => 2,
    ],
];

Options

Fuse has a lot of options to refine your search:

Basic Options

isCaseSensitive

  • Type: bool
  • Default: false

Indicates whether comparisons should be case sensitive.


includeScore

  • Type: bool
  • Default: false

Whether the score should be included in the result set. A score of 0 indicates a perfect match, while a score of 1 indicates a complete mismatch.


includeMatches

  • Type: bool
  • Default: false

Whether the matches should be included in the result set. When true, each record in the result set will include the indices of the matched characters. These can consequently be used for highlighting purposes.


minMatchCharLength

  • Type: int
  • Default: 1

Only the matches whose length exceeds this value will be returned. (For instance, if you want to ignore single character matches in the result, set it to 2).


shouldSort

  • Type: bool
  • Default: true

Whether to sort the result list, by score.


findAllMatches

  • Type: bool
  • Default: false

When true, the matching function will continue to the end of a search pattern even if a perfect match has already been located in the string.


keys

  • Type: array
  • Default: []

List of keys that will be searched. This supports nested paths, weighted search, searching in arrays of strings and objects.


Fuzzy Matching Options

location

  • Type: int
  • Default: 0

Determines approximately where in the text is the pattern expected to be found.


threshold

  • Type: float
  • Default: 0.6

At what point does the match algorithm give up. A threshold of 0.0 requires a perfect match (of both letters and location), a threshold of 1.0 would match anything.


distance

  • Type: int
  • Default: 100

Determines how close the match must be to the fuzzy location (specified by location). An exact letter match which is distance characters away from the fuzzy location would score as a complete mismatch. A distance of 0 requires the match be at the exact location specified. A distance of 1000 would require a perfect match to be within 800 characters of the location to be found using a threshold of 0.8.


ignoreLocation

  • Type: bool
  • Default: false

When true, search will ignore location and distance, so it won't matter where in the string the pattern appears.

Tip: The default options only search the first 60 characters. This should suffice if it is reasonably expected that the match is within this range. To modify this behavior, set the appropriate combination of location, threshold, distance (or ignoreLocation).

To better understand how these options work together, read about Fuse.js' Scoring Theory.


Advanced Options

useExtendedSearch

  • Type: bool
  • Default: false

When true, it enables the use of unix-like search commands. See example.


getFn

  • Type: callable
  • Default: source

The function to use to retrieve an object's value at the provided path. The default will also search nested paths.


sortFn

  • Type: callable
  • Default: source

The function to use to sort all the results. The default will sort by ascending relevance score, ascending index.


ignoreFieldNorm

  • Type: bool
  • Default: false

When true, the calculation for the relevance score (used for sorting) will ignore the field-length norm.


Tip: The only time it makes sense to set ignoreFieldNorm to true is when it does not matter how many terms there are, but only that the query term exists.

fieldNormWeight

  • Type: float
  • Default: 1

Determines how much the field-length norm affects scoring. A value of 0 is equivalent to ignoring the field-length norm. A value of 0.5 will greatly reduce the effect of field-length norm, while a value of 2.0 will greatly increase it.


Global Config

You can access and manipulate default values of all options above via the config method:

// Get an associative array of all options listed above
Fuse::config();

// Merge associative array of options into default config
Fuse::config(['shouldSort' => false]);

// Get single default option
Fuse::config('shouldSort');

// Set single default option
Fuse::config('shouldSort', false);

Methods

The following methods are available on each Fuse\Fuse instance:


search

Searches the entire collection of documents, and returns a list of search results.

public function search(mixed $pattern, ?array $options): array

The $pattern can be one of:

The $options:

  • limit (type: int): Denotes the max number of returned search results.

setCollection

Set/replace the entire collection of documents. If no index is provided, one will be generated.

public function setCollection(array $docs, ?\Fuse\Core\FuseIndex $index): void

Example:

$fruits = ['apple', 'orange'];
$fuse = new Fuse($fruits);

$fuse->setCollection(['banana', 'pear']);

add

Adds a doc to the collection and update the index accordingly.

public function add(mixed $doc): void

Example:

$fruits = ['apple', 'orange'];
$fuse = new Fuse($fruits);

$fuse->add('banana');

sizeof($fruits); // => 3

remove

Removes all documents from the list which the predicate returns truthy for, and returns an array of the removed docs. The predicate is invoked with two arguments: ($doc, $index).

public function remove(?callable $predicate): array

Example:

$fruits = ['apple', 'orange', 'banana', 'pear'];
$fuse = new Fuse($fruits);

$results = $fuse->remove(fn($doc) => $doc === 'banana' || $doc === 'pear');
sizeof($fuse->getCollection()); // => 2
$results; // => ['banana', 'pear']

removeAt

Removes the doc at the specified index.

public function removeAt(int $index): void

Example:

$fruits = ['apple', 'orange', 'banana', 'pear'];
$fuse = new Fuse($fruits);

$fuse->removeAt(1);

$fuse->getCollection(); // => ['apple', 'banana', 'pear']

getIndex

Returns the generated Fuse index.

public function getIndex(): \Fuse\Core\FuseIndex

Example:

$fruits = ['apple', 'orange', 'banana', 'pear'];
$fuse = new Fuse($fruits);

$fuse->getIndex()->size(); // => 4

Indexing

The following methods are available on each Fuse\Fuse instance:


Fuse::createIndex

Pre-generate the index from the list, and pass it directly into the Fuse instance. If the list is (considerably) large, it speeds up instantiation.

public static function createIndex(array $keys, array $docs, array $options = []): \Fuse\Core\FuseIndex

Example:

$list = [ ... ]; // See the example from the 'Usage' section
$options = [ 'keys' => [ 'title', 'author.firstName' ] ];

// Create the Fuse index
$myIndex = Fuse::createIndex($options['keys'], $list);

// Initialize Fuse with the index
$fuse = new Fuse($list, $options, $myIndex);

Fuse::parseIndex

Parses a JSON-serialized Fuse index.

public static function parseIndex(array $data, array $options = []): \Fuse\Core\FuseIndex

Example:

// (1) When the data is collected

$list = [ ... ]; // See the example from the 'Usage' section
$options = [ 'keys' => [ 'title', 'author.firstName' ] ];

// Create the Fuse index
$myIndex = Fuse::createIndex($options['keys'], $list);

// Serialize and save it
file_put_contents('fuse-index.json', json_encode($myIndex));


// (2) When the search is needed

// Load and deserialize index to an array
$fuseIndex = json_decode(file_get_contents('fuse-index.json'), true);
$myIndex = Fuse::parseIndex($fuseIndex);

// Initialize Fuse with the index
$fuse = new Fuse($list, $options, $myIndex);

Differences with Fuse.js

  Fuse.js PHP Fuse
Get Fuse Version Fuse.version
Access global configuration Fuse.config property Fuse::config method
List modification Using fuse.add() etc. modifies the original list passed to the new Fuse constructor. In PHP, arrays are a primitive data type, which means that your original list is never modified by Fuse. To receive the current list after adding/removing items, the $fuse->getCollection() method can be used.

Development

Project Scope

Please note that I'm striving for feature parity with Fuse.js and therefore will add neither features nor fixes to the search logic that are not reflected in Fuse.js itself.

If you have any issues with search results that are not obviously bugs in this PHP port, and you happen to know JavaScript, please check if your use case works correctly in the online demo of Fuse.js as that is the canonical Fuse implementation. If the issue appears there as well, please open an issue in their repo.

Setup

To start development on Fuse, you need git, PHP (≥ 7.4) and Composer.

Since code is formatted using Prettier, it's also recommended to have Node.js/npm installed as well as using an editor which supports Prettier formatting.

Clone the repository and cd into it:

git clone https://github.com/loilo/fuse.git
cd fuse

Install Composer dependencies:

composer install

Install npm dependencies (optional but recommended). This is only needed for code formatting as npm dependencies include Prettier plugins used by this project.

npm ci

Quality Assurance

There are different kinds of code checks in place for this project. All of these are run when a pull request is submitted but can also be run locally:

Command Purpose Description
vendor/bin/phpcs check code style Run PHP_CodeSniffer to verify that the Fuse source code abides by the PSR-12 coding style.
vendor/bin/psalm static analysis Run Psalm against the codebase to avoid type-related errors and unsafe coding patterns.
vendor/bin/phpunit check program logic Run all PHPUnit tests from the test folder.

Contributing

Before submitting a pull request, please add relevant tests to the test folder.

fuse's People

Contributors

crocodile2u avatar dependabot-preview[bot] avatar dependabot[bot] avatar hjardines avatar loilo avatar shadowalker89 avatar vinkla 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  avatar  avatar  avatar  avatar  avatar  avatar

fuse's Issues

Exceeding the max pattern length in search string doesn't throw error

I've found a case whereby the threshold of 0 is not being respected.

I initialise the Fuse object like so
$fuseTest = new Fuse\Fuse([['name' => '8Th Street Clinic']], ['keys' => ['name'], 'threshold' => 0, 'includeScore' => true]);

and search for
$fuseTest->search('Tumaini Medial Clinic (Miriga Mieru West)')

I get back

 [
     [
       "item" => [
         "name" => "8Th Street Clinic",
       ],
       "score" => 0.5,
     ],
] 

Which as you can see has a score of 0.5 but is still returned.

Fuse in Drupal

I am using this project on my local Drupal build. The Fuse array does not show a "Results" index in the array after the search is complete. Can you confirm that the results of the search should be in a "Results" index in the Fuse array?

Undefinex index 0 when using 'matchAllTokens' option

"type": "Whoops\\Exception\\ErrorException",
"message": "Undefined offset: 0",
"file": "/var/www/app/vendor/loilo/fuse/src/Fuse.php",
"line": 231,

The line is:
$averageScore = $scores[0];

But when the option matchAllTokens is used, $scores will be empty (in my tests)

I just initialized $averageScore = 0 and then check that index 0 is set in the $scores array. Seems ok?
I can make a pull request

PHP 5.3

Can I use this with php 5.3 if I change all array constructs from [] to array()?
Would there be any other 5.3 limitations?

PHP 8 support

Hi! When will be released a stable version with PHP 8 support?

Getting weighted search

Hi,
I have been playing around with Fuse for a few days now and I can't get weighted search to work. I have a needle array that looks like this:

Array
(
[1] => Array
(
[company] => Array
(
[name] => STARTUPLIFERS IN SWEDEN
[registrationid] => 55XXXX-XXXX
[id] => 1
)
),
[company] => Array
(
[name] => Google
[registrationid] => 55XXXX-XXXX
[id] => 1
),
[tags] => Array
(
[name] => Startup
)
)
......
[7992] => Array
(
[stopwords] => Array
(
[name] => Startup
)
)
...

So essentially an array with companies and I have added "stop words" in the array.

I have created my key array as follows:
'keys' => [
[
'name' => 'stopword.name',
'weight' => 1
],
[
'name' => 'company.name',
'weight' => 0.5
],
[
'name' => 'tags.name',
'weight' => 1
]
]

But when I search for the term "Startup" it does not matter how I weight my keys, the company STARTUPLIFERS is always the one with the lowest score. followed by my stop word that is a 100% match. Im using the other options for my query:
"tokenize" => true,
"matchAllTokens" => true,
"caseSensitive" => false,
"includeScore" => true,
"shouldSort" => true

It looks like this is a limitation perhaps in Fuse.js (Will try to replicate it in JS) as there is no way to force a "word boundary" search that would have helped solving this specific problem.

Error that needs to be fixed

Hi
I am using your project that it is very useful but i noticed a problem that needs to be fixed.
In Fuse.php file at line 226 your code is
$averageScore = $averageScore / $scoresLen;

I noticed that sometimes the $scoresLen in my project is 0 so the the action of division prints out an error
I suggest adding the following in order to avoid this error
if($scoresLen !=0){
$averageScore = $averageScore / $scoresLen;
}
Best Regards

Performance question

Hello,

I'm not quite sure if this is the right place to ask this, but I have a question about the performance of the search.

I currently have an array of ~81k indexes, with 3 keys each, only searching by one ("Name"). It currently takes ~3.8s to return 0 results (if I search something that doesn't match, and ~4.9s if I search something that does), with these settings:
$fuse = new \Fuse\Fuse($locationData, [ "keys" => ["Name"] , "threshold" => 0.2 , "distance" => 10 , "includeScore" => true , "includeMatches" => true ]);

My data (imported as JSON) looks like this:
Array ( [0] => Array ( [Name] => ACCOMACK [ListingCount] => 191 [Type] => county ) )

It looks like that all the time simply just comes from the size of the data, as with logging the times, I constantly got ~4s to reach the 9th match in a search. It also looks like that 'Fuse->analyze()' is run twice, as with a simple increment, the 9th match returns that it was the 160663 run of the function. Although, bypassing that (is_string was false, and is_list reactivated analyze as a string) didn't seem to improve the speed at all.

Here's an example of the timings it took to search (only logging when a match was found): http://prntscr.com/n2v14q

Is there a better format I can use for the data, or is this just a limitation of having 81k+ items to search through?

I'm not sure what other information you need, but let me know if I can provide anything else. I don't have any public page set up with this to test it against at the moment, but if you need, I can probably get that set up.

The data that I'm searching through has 5 distinct parts, so I tried also searching through each separately. When logging the time specifically, increased the total time by ~0.3s. When not logging, specifically, reduced the total time by ~0.8s (while also returning 40 more results)

PHP 7.2
Running on a DreamHost server (not sure of specs)

Thanks,
Tony.

Mixed scores for matching results.

I have matches that come through with a score of 2.2+ and then other concrete matches that show a score of 0.01 - I have some code that looks for anything over 2 and anything under 0.1, but are you aware of this?

Pattern exceed max length of 32

The pattern exceed max lenght of 32 is showing for the search query that has more than 32 characters with some unicode characters and in UTF-8 encoding. This most likely happen in BitapSearch::__construct() during the chunking of characters. Upon further checking, it seems that UTF-8 encoding has problems with PHP's substr and it might be better to use mb_substr, see topic

$query = 'send more money to my pàypàl account';
$fuzzyMatcher = new Fuse(['Sample collection only, please ignore'], []);

$fuzzyMatcher->search($query);

Search is not working well

Right now I don't know how to set up the arguments for Search method:

Keywords: roofing

Current Results:

  • Undertile External Waterproofing Membranes
  • Roofing Membrane
  • Liquid waterproofing membrane
  • External waterproofing membrane
  • Liquid waterproofing membrane
    ======================================
    Expected Results:
  • Roofing Membrane
    ======================================
    Can someone help me sort this problem out? Thanks.

includeScore

I tried to use includeScore but it behaves differently than expected: that is, it doesn't return 0 or 1, but a float > 1 for the first match and a float between 0 and 1 for any other match.

What's Wrong With My Array?

I have an array that outputs like this:

    [1106] => Array
        (
            [id] => 1106
            [url] => 3-brides-for-3-bad-boys-160488
            [description] => 0
            [title] => 3 Brides for 3 Bad Boys
            [author] => 3 Brides for 3 Bad Boys (mf)
            [author_lname] => Boys
            [language] => en
        )

    [1107] => Array
        (
            [id] => 1107
            [url] => 3-brisingr-3-12630502066281
            [description] => 0
            [title] => 3-Brisingr-3
            [author] => Unknown
            [author_lname] => 
            [language] => eng
        )

    [1108] => Array
        (
            [id] => 1108
            [url] => 3-claus-of-death-120101
            [description] => 0
            [title] => 3 Claus of Death
            [author] => Gayle Trent
            [author_lname] => Trent
            [language] => en
        )

But that results in:

PHP Notice: Undefined offset: -1 in /var/www/search/vendor/loilo/fuse/src/Bitap/matched_indices.php on line 24
PHP Notice: Undefined offset: -1 in /var/www/search/vendor/loilo/fuse/src/Bitap/matched_indices.php on line 24
PHP Notice: Undefined offset: -1 in /var/www/search/vendor/loilo/fuse/src/Bitap/matched_indices.php on line 24
PHP Notice: Undefined offset: -1 in /var/www/search/vendor/loilo/fuse/src/Bitap/matched_indices.php on line 24
PHP Notice: Undefined offset: -1 in /var/www/search/vendor/loilo/fuse/src/Bitap/matched_indices.php on line 24
PHP Notice: Undefined offset: -1 in /var/www/search/vendor/loilo/fuse/src/Bitap/matched_indices.php on line 24
PHP Notice: Undefined offset: -1 in /var/www/search/vendor/loilo/fuse/src/Bitap/matched_indices.php on line 24
PHP Notice: Undefined offset: -1 in /var/www/search/vendor/loilo/fuse/src/Bitap/matched_indices.php on line 24
PHP Notice: Undefined offset: -1 in /var/www/search/vendor/loilo/fuse/src/Bitap/matched_indices.php on line 24
PHP Notice: Undefined offset: -1 in /var/www/search/vendor/loilo/fuse/src/Bitap/matched_indices.php on line 24

I was wondering what I need to do to? I have tried putting my array into a parent array but that changes everything to:

/var/www/search# php search.php
PHP Notice: Undefined offset: 0 in /var/www/search/vendor/loilo/fuse/src/Fuse.php on line 97
PHP Notice: Undefined offset: 0 in /var/www/search/vendor/loilo/fuse/src/Fuse.php on line 123

Fuzzy search slow with large data

Hello,
We have a very large array with around 10 thousand records, each having following fields,
array(
array('name' => 'name1', 'email' => '[email protected]'),
array('name' => 'second name', 'email' => '[email protected]')
)
When searching this user's array for keys 'name' or 'email' with following settings Fuse is really slow it takes around 30, 40 seconds to find all records, I have following settings,

// initialize fuse
$settings['keys'] = array("name", "email");
$settings['distance'] = 50;
$settings['maxPatternLength'] = 16;
$settings['threshold'] = 0.6;
$fuse = new Fuse($usersArray, $settings);

// search user
$usersFound = $fuse->search("my name");

Is there a way or any setting which will help me make Fuse work faster? I tried same with JS version of Fuse and it was not that much slower took about few seconds to search for 10 thousand records

Error when using an array with missing keys

When you pass an array with a missing key, something like this

$list = [
0 => ['name' => "Jack"],
2 => ['name' => "John"] 
];

As you can see there is no 1 key

This will cause this error

ErrorException : Undefined offset: 1
 F:\projects\quick-conference\src\quick-conference\vendor\loilo\fuse\src\Fuse.php:131
 F:\projects\quick-conference\src\quick-conference\vendor\loilo\fuse\src\Fuse.php:64

For me the solution is the wrap the $list variable in a array_values() and use the return of the function to the Fuse library.

I think that the library should handle the missing keys, because not all the time it's going to receive arrays with ordered keys.

In my situation i'm passing a list of names from the form submitted by users.
Users can leave empty rows in between which causes the have gaps in the array keys

not really sure about the settings of arguments with this repo

So right now I have a requirement about Fuzzy search like this. However, I'm really confused about the Settings of blow list.

  • threshold: (type: float, default: 0.6)
  • distance (type: int, default: 100)
  • ... and other lots of arguments here

This is our pattern for searching:

To simplify our scope, if we are searching the phrase "A B C":
1.       All permutations of “A B C” allowed:
1.1   3 words in any order, e.g. A C B, B C A
1.2   3 words in any order +any other word(s), e.g. A C B+XY, B C A+Z
1.3   Any of the 2 words, in any order, e.g. A B, C B, C A
1.4   Any of the 2 words, in any order + any other word(s), e.g. A B+XY, CB+XZ, CA+Z
 
2.       Permutations of “A B C” not allowed:
2.1   Single word only, e.g. Just A or B, or C
2.2   One word + any other word(s), e.g. A+XY, B+YZ, C+Z

Thanks for your time from you guys.

preg_match fails when string has "/" in it

loilo/fuse/src/Bitap/regex_search.php: 7

Example trace

  Fuse\Bitap\regex_search("R. Aazyol", "Atelier Richard Woodman Burbridge/Harrods", " +")
  vendor/loilo/fuse/src/Bitap/Bitap.php : 63

Can't get a result

I have been trying to get a result from a search of my data set but I have yet to be able to get a result. I plugged my data set and options into the Fuse.js demo page and was able to get the expected results.

My data(small portion of it):

array(169) {
  [0]=>
  array(4) {
    ["name"]=>
    string(17) "Grace Watson Hall"
    ["description"]=>
    string(7) "GWH/025"
    ["mdo_id"]=>
    string(1) "3"
    ["coordinates"]=>
    array(2) {
      [0]=>
      float(-77.66901)
      [1]=>
      float(43.083652)
    }
  }
  [1]=>
  array(4) {
    ["name"]=>
    string(15) "Wallace Library"
    ["description"]=>
    string(7) "WAL/005"
    ["mdo_id"]=>
    string(1) "5"
    ["coordinates"]=>
    array(2) {
      [0]=>
      float(-77.676315)
      [1]=>
      float(43.083927)
    }
  }

initialization and search

$options = ["keys" => ["name","description","mdo_id"]];
$fuse =  new \Fuse\Fuse($data,$options);
$result = $fuse->search("Grace Watson Hall");
//result returns nothing and I get a 500 error

Modification to "analyze" method in Fuse Class

-- // Guard against "Undefined offset: 0" Error when $scoresLen == 0
-- // Guard against "Division By Zero" Error when $scoresLen == 0

$scoresLen = sizeof($scores);

            // Guide against "Undefined offset: 0" Error when $scoresLen == 0
            if($scoresLen > 0)
            $averageScore = $scores[0];
            
            for($i = 1; $i < $scoresLen; $i++) {
                $averageScore += $scores[$i];
            }
            
            // Guide against "Division By Zero" Error when $scoresLen == 0
            if($scoresLen > 0)
            $averageScore /= $scoresLen;

Thanks for the PHP port. Its very useful for me. Please don't forget to Update us when Fuse.js has major changes. Regards

`limit` option does not work

No matter what limit is specified, the results array return all the items. Basically limit option is not taken into effect.

$list = [
    [
        'title' => "Old Man's War",
        'author' => 'John Scalzi',
    ],
    [
        'title' => 'The Lock Artist',
        'author' => 'Steve Hamilton',
    ],
    [
        'title' => 'HTML5',
        'author' => 'Remy Sharp',
    ],
    [
        'title' => 'Right Ho Jeeves',
        'author' => 'P.D Woodhouse',
    ],
];

$options = [
    'keys' => ['author'],
    'limit' => 1,
];

$fuse = new \Fuse\Fuse($list, $options);

echo "<pre>";
print_r($fuse->search('o'));
echo "</pre>";




$list = [
    [
        'title' => "Old Man's War",
        'author' => 'John Scalzi',
    ],
    [
        'title' => 'The Lock Artist',
        'author' => 'Steve Hamilton',
    ],
    [
        'title' => 'HTML5',
        'author' => 'Remy Sharp',
    ],
    [
        'title' => 'Right Ho Jeeves',
        'author' => 'P.D Woodhouse',
    ],
];

$options = [
    'keys' => ['author'],
    'limit' => 2,
];

$fuse = new \Fuse\Fuse($list, $options);

echo "<pre>";
print_r($fuse->search('o'));
echo "</pre>";

Both examples above return the same result:

Array
(
    [0] => Array
        (
            [item] => Array
                (
                    [title] => Old Man's War
                    [author] => John Scalzi
                )

            [refIndex] => 0
        )

    [1] => Array
        (
            [item] => Array
                (
                    [title] => Right Ho Jeeves
                    [author] => P.D Woodhouse
                )

            [refIndex] => 3
        )

    [2] => Array
        (
            [item] => Array
                (
                    [title] => The Lock Artist
                    [author] => Steve Hamilton
                )

            [refIndex] => 1
        )

)

Is this a bug or am I missing something?

Performance

Dear,

The library is great and the precision is very good. However, i have performances issues.

I'm running the tool on a list of 15,000 entries (2-3 words per entry, 1 field only) that i want to fuzzy against ifself.
Here is my configuration

$thresholdFuzzy = 0.6; // lower down that value to be more strict. 0 = exact match
$FuzzyParameters = [
  "keys" => [ "keywordName"],
  "includeScore" => true,			
  "maxPatternLength" => 32,
  "threshold" => $thresholdFuzzy,
  "minMatchCharLength" => 1
];

$fuse = new \Fuse\Fuse($dataToFuzzyNoExactMatch, $FuzzyParameters);
$results = $fuse->search($kw);

I executed a loop of fuzzy on (the first) 100 entries compared to 15,000.
It took 500 seconds to compute.

Any idea how i can improve the speed please?

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.