From af5f329442dd556efb10e9d5081f4082e4787051 Mon Sep 17 00:00:00 2001 From: iget-master Date: Tue, 12 May 2015 18:28:39 -0300 Subject: [PATCH 1/2] Update SearchableTrait.php --- src/SearchableTrait.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/SearchableTrait.php b/src/SearchableTrait.php index 6524034..2f504e4 100644 --- a/src/SearchableTrait.php +++ b/src/SearchableTrait.php @@ -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; @@ -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; @@ -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; } @@ -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; } From 467b45e4cf35c1ec984ed84024f743737bf8fe16 Mon Sep 17 00:00:00 2001 From: iget-master Date: Tue, 12 May 2015 18:55:39 -0300 Subject: [PATCH 2/2] Update README.md --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index db66d02..5a82bde 100755 --- a/README.md +++ b/README.md @@ -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