Skip to content

Commit

Permalink
Merge pull request #9 from billmn/patch-2
Browse files Browse the repository at this point in the history
Code improvements
  • Loading branch information
nicolaslopezj committed Oct 22, 2014
2 parents 3c2e49b + 0f3972d commit 3ca9742
Showing 1 changed file with 44 additions and 28 deletions.
72 changes: 44 additions & 28 deletions src/Nicolaslopezj/Searchable/SearchableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
*/
trait SearchableTrait
{

/**
* Makes the search process
*
* @param $query
* @param $search
* @return mixed
*/
public function scopeSearch($query, $search) {

public function scopeSearch($query, $search)
{
$query->select($this->getTable() . '.*');
$this->makeJoins($query);

Expand All @@ -25,16 +25,16 @@ public function scopeSearch($query, $search) {
return $query;
}

$relevance_count = 0;
$words = explode(' ', $search);

$selects = [];
foreach ($this->getColumns() as $column => $relevance) {
$relevance_count = 0;

foreach ($this->getColumns() as $column => $relevance)
{
$relevance_count += $relevance;

$queries = $this->getSearchQueriesForColumn($column, $relevance, $words);
foreach ($queries as $select) {
$selects[] = $select;
}
$selects = array_merge($queries);
}

$this->addSelectsToQuery($query, $selects);
Expand All @@ -47,79 +47,93 @@ public function scopeSearch($query, $search) {

/**
* Returns the search columns
*
* @return array
*/
protected function getColumns() {
protected function getColumns()
{
return $this->searchable['columns'];
}

/**
* Returns the tables that has to join
*
* @return array
*/
protected function getJoins() {
if (!array_key_exists('joins', $this->searchable)) {
return [];
}
return $this->searchable['joins'];
protected function getJoins()
{
return array_get($this->searchable, 'joins', []);
}

/**
* Adds the join sql to the query
*
* @param $query
*/
protected function makeJoins(&$query) {
foreach ($this->getJoins() as $table => $keys) {
protected function makeJoins(&$query)
{
foreach ($this->getJoins() as $table => $keys)
{
$query->leftJoin($table, $keys[0], '=', $keys[1]);
}
}

/**
* Make the query dont repeat the results
*
* @param $query
*/
protected function makeGroupBy(&$query) {
$primary_key = $this->primaryKey;
$query->groupBy($primary_key);
protected function makeGroupBy(&$query)
{
$query->groupBy($this->primaryKey);
}

/**
* Puts all the select clauses to the main query
*
* @param $query
* @param $selects
*/
protected function addSelectsToQuery(&$query, $selects) {
$selects = new Expression(join(' + ', $selects) . ' as relevance');
protected function addSelectsToQuery(&$query, $selects)
{
$selects = new Expression(implode(' + ', $selects) . ' as relevance');
$query->addSelect($selects);
}

/**
* Adds relevance filter to the query
*
* @param $query
* @param $relevance_count
*/
protected function filterQueryWithRelevace(&$query, $relevance_count) {
protected function filterQueryWithRelevace(&$query, $relevance_count)
{
$query->havingRaw('relevance > ' . $relevance_count);
$query->orderBy('relevance', 'desc');
}

/**
* Returns the search queries for the specified column
*
* @param $column
* @param $relevance
* @param $words
* @return array
*/
protected function getSearchQueriesForColumn($column, $relevance, $words) {
protected function getSearchQueriesForColumn($column, $relevance, $words)
{
$queries = [];

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

return $queries;
}

/**
* Returns the sql string for the parameters
*
* @param $column
* @param $relevance
* @param $words
Expand All @@ -129,15 +143,17 @@ protected function getSearchQueriesForColumn($column, $relevance, $words) {
* @param string $post_word
* @return string
*/
protected function getSearchQuery($column, $relevance, $words, $compare, $relevance_multiplier, $pre_word = '', $post_word = '') {
protected function getSearchQuery($column, $relevance, $words, $compare, $relevance_multiplier, $pre_word = '', $post_word = '')
{
$fields = [];
foreach ($words as $word) {

foreach ($words as $word)
{
$fields[] = $column . " " . $compare . " '" . $pre_word . $word . $post_word . "'";
}

$fields = join(' || ', $fields);
$fields = implode(' || ', $fields);

return 'if(' . $fields . ', ' . $relevance * $relevance_multiplier . ', 0)';
}

}

0 comments on commit 3ca9742

Please sign in to comment.