Skip to content

Commit

Permalink
Addition of new AmbiSearchDriver to allow more intuitive text search
Browse files Browse the repository at this point in the history
  • Loading branch information
TomLingham committed Apr 30, 2017
1 parent b275da4 commit 6dcc3e5
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 11 deletions.
4 changes: 4 additions & 0 deletions config/searchy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
'class' => 'TomLingham\Searchy\SearchDrivers\FuzzySearchDriver',
],

'ambi' => [
'class' => 'TomLingham\Searchy\SearchDrivers\AmbiSearchDriver',
],

'ufuzzy' => [
'class' => 'TomLingham\Searchy\SearchDrivers\FuzzySearchUnicodeDriver',
],
Expand Down
33 changes: 33 additions & 0 deletions src/Matchers/DeterminerMatcher.php
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;
}
}
14 changes: 14 additions & 0 deletions src/Matchers/InsideStartOfWordsMatcher.php
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 . '%';
}
}
19 changes: 19 additions & 0 deletions src/Matchers/PositionInSearchMatcher.php
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;
}
}
15 changes: 15 additions & 0 deletions src/SearchDrivers/AmbiSearchDriver.php
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,
];
}
5 changes: 1 addition & 4 deletions src/SearchDrivers/FuzzySearchDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

class FuzzySearchDriver extends BaseSearchDriver
{
/**
* @var array
*/
protected $matchers = [
\TomLingham\Searchy\Matchers\ExactMatcher::class => 100,
\TomLingham\Searchy\Matchers\StartOfStringMatcher::class => 50,
Expand All @@ -16,5 +13,5 @@ class FuzzySearchDriver extends BaseSearchDriver
\TomLingham\Searchy\Matchers\StudlyCaseMatcher::class => 32,
\TomLingham\Searchy\Matchers\InStringMatcher::class => 30,
\TomLingham\Searchy\Matchers\TimesInStringMatcher::class => 8,
];
];
}
5 changes: 1 addition & 4 deletions src/SearchDrivers/FuzzySearchUnicodeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

class FuzzySearchUnicodeDriver extends BaseSearchDriver
{
/**
* @var array
*/
protected $matchers = [
\TomLingham\Searchy\Matchers\ExactMatcher::class => 100,
\TomLingham\Searchy\Matchers\StartOfStringMatcher::class => 50,
Expand All @@ -16,5 +13,5 @@ class FuzzySearchUnicodeDriver extends BaseSearchDriver
\TomLingham\Searchy\Matchers\StudlyCaseUnicodeMatcher::class => 32,
\TomLingham\Searchy\Matchers\InStringMatcher::class => 30,
\TomLingham\Searchy\Matchers\TimesInStringMatcher::class => 8,
];
];
}
3 changes: 0 additions & 3 deletions src/SearchDrivers/SimpleSearchDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

class SimpleSearchDriver extends BaseSearchDriver
{
/**
* @var array
*/
protected $matchers = [
\TomLingham\Searchy\Matchers\ExactMatcher::class => 100,
\TomLingham\Searchy\Matchers\StartOfStringMatcher::class => 50,
Expand Down

0 comments on commit 6dcc3e5

Please sign in to comment.