Skip to content

Commit

Permalink
Merge pull request #55 from iget-master/master
Browse files Browse the repository at this point in the history
Improved EntireWord Search
  • Loading branch information
nicolaslopezj committed May 12, 2015
2 parents 81b5478 + 467b45e commit edaae62
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,18 @@ class User extends \Eloquent

####Search:
```php
$search = User::search('Sed neque labore')->get();
$search = User::search('Sed neque labore', null, true)->get();
```

####Result:
```sql
select `users`.*,

-- If third parameter is set as true, it will check if the column starts with the search
-- if then it adds relevance * 30
-- this ensures that relevant results will be at top
(case when first_name LIKE 'Sed neque labore%' then 300 else 0 end) +

-- For each column you specify makes 3 "ifs" containing
-- each word of the search input and adds relevace to
-- the row
Expand Down
26 changes: 13 additions & 13 deletions src/SearchableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,9 @@ public function scopeSearch(Builder $q, $search, $threshold = null, $entireText
return $q;
}

$search = strtolower($search);
$search = strtolower(trim($search));
$words = explode(' ', $search);
if ( $entireText === true )
{
array_unshift($words, $search);
}


$selects = [];
$this->search_bindings = [];
$relevance_count = 0;
Expand All @@ -52,6 +47,12 @@ public function scopeSearch(Builder $q, $search, $threshold = null, $entireText
{
$relevance_count += $relevance;
$queries = $this->getSearchQueriesForColumn($query, $column, $relevance, $words);

if ( $entireText === true )
{
$queries[] = $this->getSearchQuery($query, $column, $relevance, [$search], 30, '', '%');
}

foreach ($queries as $select)
{
$selects[] = $select;
Expand Down Expand Up @@ -195,13 +196,11 @@ protected function filterQueryWithRelevance(Builder $query, array $selects, $rel
*/
protected function getSearchQueriesForColumn(Builder $query, $column, $relevance, array $words)
{
$like_comparator = $this->getDatabaseDriver() == 'pgsql' ? 'ILIKE' : 'LIKE';

$queries = [];

$queries[] = $this->getSearchQuery($query, $column, $relevance, $words, $like_comparator, 15);
$queries[] = $this->getSearchQuery($query, $column, $relevance, $words, $like_comparator, 5, '', '%');
$queries[] = $this->getSearchQuery($query, $column, $relevance, $words, $like_comparator, 1, '%', '%');
$queries[] = $this->getSearchQuery($query, $column, $relevance, $words, 15);
$queries[] = $this->getSearchQuery($query, $column, $relevance, $words, 5, '', '%');
$queries[] = $this->getSearchQuery($query, $column, $relevance, $words, 1, '%', '%');

return $queries;
}
Expand All @@ -219,13 +218,14 @@ protected function getSearchQueriesForColumn(Builder $query, $column, $relevance
* @param string $post_word
* @return string
*/
protected function getSearchQuery(Builder $query, $column, $relevance, array $words, $compare, $relevance_multiplier, $pre_word = '', $post_word = '')
protected function getSearchQuery(Builder $query, $column, $relevance, array $words, $relevance_multiplier, $pre_word = '', $post_word = '')
{
$like_comparator = $this->getDatabaseDriver() == 'pgsql' ? 'ILIKE' : 'LIKE';
$cases = [];

foreach ($words as $word)
{
$cases[] = $this->getCaseCompare($column, $compare, $relevance * $relevance_multiplier);
$cases[] = $this->getCaseCompare($column, $like_comparator, $relevance * $relevance_multiplier);
$this->search_bindings[] = $pre_word . $word . $post_word;
}

Expand Down

0 comments on commit edaae62

Please sign in to comment.