-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addition of new AmbiSearchDriver to allow more intuitive text search
- Loading branch information
1 parent
b275da4
commit 6dcc3e5
Showing
8 changed files
with
87 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace TomLingham\Searchy\Matchers; | ||
|
||
/** | ||
* Mathes the search term preceded by common determiners and applies the appropriate multiplier | ||
* if it matches. | ||
* | ||
* For example, a search for 'gar', will match 'The Garage Band', or 'a garden'. | ||
* | ||
* Class DeterminerMatcher | ||
*/ | ||
class DeterminerMatcher extends BaseMatcher | ||
{ | ||
|
||
public function buildQueryString($column, $searchString) | ||
{ | ||
$determiners = [ | ||
'the', | ||
'a', 'an', | ||
'here', 'there', 'this', 'that', 'these', 'those', | ||
'i', | ||
]; | ||
|
||
$queries = array_map(function($d) use ($searchString, $column) { | ||
return "IF($column LIKE '$d $searchString%', $this->multiplier, 0)"; | ||
}, $determiners); | ||
|
||
$query = implode($queries, ' + '); | ||
|
||
return $query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace TomLingham\Searchy\Matchers; | ||
|
||
|
||
class InsideStartOfWordsMatcher extends BaseMatcher | ||
{ | ||
protected $operator = 'LIKE'; | ||
|
||
public function formatSearchString($searchString) | ||
{ | ||
return '% ' . $searchString . '%'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace TomLingham\Searchy\Matchers; | ||
|
||
class PositionInSearchMatcher extends BaseMatcher | ||
{ | ||
public function buildQueryString($column, $searchString) | ||
{ | ||
$query = "IF( | ||
POSITION(' $searchString' IN $column) = 0, | ||
0, | ||
$this->multiplier - ROUND( | ||
(POSITION('$searchString' IN $column) - 1) / CHAR_LENGTH($column) * $this->multiplier | ||
) | ||
)"; | ||
|
||
return $query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace TomLingham\Searchy\SearchDrivers; | ||
|
||
class AmbiSearchDriver extends BaseSearchDriver | ||
{ | ||
protected $matchers = [ | ||
\TomLingham\Searchy\Matchers\ExactMatcher::class => 150, | ||
\TomLingham\Searchy\Matchers\DeterminerMatcher::class => 110, | ||
\TomLingham\Searchy\Matchers\PositionInSearchMatcher::class => 100, | ||
\TomLingham\Searchy\Matchers\StartOfStringMatcher::class => 90, | ||
\TomLingham\Searchy\Matchers\InsideStartOfWordsMatcher::class => 50, | ||
\TomLingham\Searchy\Matchers\StartOfWordsMatcher::class => 35, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters