Skip to content

Commit 75a1459

Browse files
committed
Add multiple level of fuzziness
1 parent 600c40c commit 75a1459

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
### Added
1616

1717
- Detection of syntax error from Redis response
18+
- Allow multiple level of fuzziness
1819

1920
## [1.1.0]
2021

src/Search/QueryBuilder/FuzzyWord.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,30 @@
2121

2222
namespace MacFJA\RediSearch\Search\QueryBuilder;
2323

24+
use MacFJA\RediSearch\Search\Exception\OutOfRangeLevenshteinDistanceException;
2425
use function sprintf;
26+
use function str_repeat;
2527

2628
class FuzzyWord implements PartialQuery
2729
{
2830
/** @var string */
2931
private $word;
3032

31-
public function __construct(string $word)
33+
/** @var int */
34+
private $levenshteinDistance;
35+
36+
public function __construct(string $word, int $levenshteinDistance = 1)
3237
{
38+
if ($levenshteinDistance < 1 || $levenshteinDistance > 3) {
39+
throw new OutOfRangeLevenshteinDistanceException($levenshteinDistance);
40+
}
3341
$this->word = $word;
42+
$this->levenshteinDistance = $levenshteinDistance;
3443
}
3544

3645
public function render(): string
3746
{
38-
return sprintf('%%%s%%', $this->word);
47+
return sprintf('%1$s%2$s%1$s', str_repeat('%%', $this->levenshteinDistance), $this->word);
3948
}
4049

4150
public function includeSpace(): bool

0 commit comments

Comments
 (0)