Skip to content
This repository was archived by the owner on Apr 1, 2024. It is now read-only.

Commit 5b15fcf

Browse files
Merge pull request #5 from robertmarney/master
Basic Min Score Threshold
2 parents 8c20653 + cfdbfd2 commit 5b15fcf

4 files changed

Lines changed: 55 additions & 1 deletion

File tree

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,23 @@ User::whereFuzzy('name', 'jd')
8989
->orderBy('relevance_email', 'desc')
9090
->first();
9191
```
92+
### Setting Minimum Match Threshold
93+
94+
When using quest an overall score will be assigned to each row `_fuzzy_relevance_` with a range of 0-100.
95+
96+
You can enforce a minimum match standard and limit results returned by using `->atLeastFuzzy()`
97+
98+
```php
99+
User::whereFuzzy('name', 'jd')
100+
->atLeastFuzzy(70)
101+
->first();
102+
103+
// Equivalent to:
104+
105+
User::whereFuzzy('name', 'jd')
106+
->having('_fuzzy_relevance_', '>', 70)
107+
->first();
108+
```
92109

93110
## Limitations
94111

@@ -104,4 +121,4 @@ If you'd like to support the development of Quest, then please consider [sponsor
104121

105122
## License
106123

107-
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
124+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

src/Macros/AtLeast.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Quest\Macros;
4+
5+
use Illuminate\Database\Query\Builder;
6+
7+
class AtLeast
8+
{
9+
/**
10+
* Construct a fuzzy search expression.
11+
*
12+
**/
13+
public static function make(Builder $builder, int $minScore) : Builder
14+
{
15+
$builder->having('_fuzzy_relevance_', '>', $minScore);
16+
return $builder;
17+
}
18+
}

src/ServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Quest;
44

55
use Closure;
6+
use Quest\Macros\AtLeast;
67
use Quest\Macros\WhereFuzzy;
78
use Quest\Macros\OrderByFuzzy;
89
use Illuminate\Database\Query\Builder;
@@ -40,5 +41,7 @@ public function boot(): void
4041

4142
return WhereFuzzy::makeOr($this, $field, $value);
4243
});
44+
45+
Builder::macro('atLeastFuzzy', fn($minScore) => AtLeast::make($this, $minScore));
4346
}
4447
}

tests/PackageTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,20 @@ public function it_can_perform_an_eloquent_fuzzy_and_search_with_fuzzy_order()
174174

175175
$this->assertEquals('Jane Doe', $results->first()->name);
176176
}
177+
178+
/** @test */
179+
public function it_can_limit_minimum_score()
180+
{
181+
$results = User::whereFuzzy('name', 'joh Do')
182+
->atLeastFuzzy(65)
183+
->get();
184+
185+
$this->assertEquals('John Doe', $results->first()->name);
186+
187+
$results = User::whereFuzzy('name', 'joh Do')
188+
->atLeastFuzzy(70)
189+
->get();
190+
191+
$this->assertCount(0, $results);
192+
}
177193
}

0 commit comments

Comments
 (0)