Skip to content

Commit

Permalink
Merge pull request #3 from ajcastro/1.2.1
Browse files Browse the repository at this point in the history
Simplify and improve codebase
  • Loading branch information
ajcastro authored Jun 20, 2019
2 parents 2c8aacd + 690068a commit b85f29e
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 90 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ Post::isColumnValid(request('sort_by'));
Post::getTableColumns();
```

## Warning

Calling `select()` after `search()` will overwrite `sort_index` field, so it is recommended to call `select()`
before `search()` which is also the normal case.

## Credits

- Ray Anthony Madrona [@raymadrona](https://github.com/raymadrona), for the tips on using MySQL `LOCATE()` for sort relevance.
Expand Down
65 changes: 56 additions & 9 deletions src/BaseSearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

abstract class BaseSearchQuery extends BaseGridQuery
{
use SortTrait;

/**
* Search operator.
* Whether to use where or having in query to compare columns against search string.
Expand All @@ -26,14 +24,12 @@ abstract class BaseSearchQuery extends BaseGridQuery
protected $searchStr;

/**
* Prepare and return the searchable query.
* If searching will be sorted by sort_index.
* This is the relevance score of the search string.
*
* @return \Illuminate\Database\Eloquent\Builder
* @var bool
*/
protected function searchableQuery()
{
return $this->query();
}
protected $sort = true;

/**
* Apply a search query.
Expand All @@ -54,7 +50,7 @@ public function search($searchStr)
public function searcher()
{
return new SublimeSearch(
$this->searchableQuery(),
$this->query(),
$this->columns(),
$this->sort,
$this->searchOperator
Expand Down Expand Up @@ -83,4 +79,55 @@ public function setSearchOperator($searchOperator)

return $this;
}

/**
* Alias of sortByRelevance.
*
* @param bool $bool
* @return $this
*/
public function sort($sort = true)
{
return $this->sortByRelevance($sort);
}

/**
* Set sort boolean.
*
* @param bool $bool
* @return $this
*/
public function sortByRelevance($sort = true)
{
$this->sort = $sort;

return $this;
}

/**
* Whether this search query should sort by relevance with key of `sort_index`.
*
* @return boolean
*/
public function shouldSortByRelevance()
{
return $this->sort;
}

/**
* Apply sorting query by relevance to the search.
* By default using mysql locate function.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $searchStr
* @return \Illuminate\Database\Eloquent\Builder
*/
public function applySortByRelevance()
{
if (!method_exists($this, 'sortColumns')) {
throw new \Exception("Sort by relevance requires sortColumns() method.");
}

SortByRelevance::sort($this->query, $this->sortColumns(), $this->searchStr);
}
}
4 changes: 4 additions & 0 deletions src/Search/SublimeSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public function search($searchStr)
$method = $this->searchOperator.'Raw';
$query = $this->query()->{$method}('('.join(' OR ', $conditions).')');

if ($this->shouldSortByRelevance()) {
$this->applySortByRelevance();
}

return $query;
}

Expand Down
43 changes: 27 additions & 16 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ trait Searchable

protected $sortByRelevance = true;

protected $searchQuery;

/**
* Return the searchable columns for this model's table.
*
Expand Down Expand Up @@ -109,39 +111,46 @@ protected function applySearchableJoins($query)
*
* @return mixed|\AjCastro\Searchable\Search\SublimeSearch
*/
public static function searchQuery()
public function searchQuery()
{
$model = new static;
if ($this->searchQuery) {
return $this->searchQuery;
}

if (method_exists($model, 'defaultSearchQuery')) {
return $model->defaultSearchQuery();
if (method_exists($this, 'defaultSearchQuery')) {
return $this->searchQuery = $this->defaultSearchQuery();
}

return new SublimeSearch($model, $model->searchableColumns(), true, 'where');
return $this->searchQuery = new SublimeSearch($this, $this->searchableColumns(), $this->sortByRelevance, 'where');
}

/**
* Set the model's search query.
*
* @param \AjCastro\Searchable\BaseSearchQuery $searchQuery
*/
public function setSearchQuery($searchQuery)
{
$this->searchQuery = $searchQuery;

return $this;
}

/**
* Apply search in the query.
*
* @param query $query
* @param string $search
* @param \AjCastro\Searchable\BaseSearchQuery $searchQuery
*
* @return void
*/
public function scopeSearch($query, $search, $searchQuery = null)
public function scopeSearch($query, $search)
{
if (is_null($searchQuery)) {
$this->applySearchableJoins($query);
}

$searchQuery = $searchQuery ?: static::searchQuery();
$this->applySearchableJoins($query);

$searchQuery->setQuery($query)->search($search)->select($this->getTable().'.*');
$query->select($this->getTable().'.*');

if ($query->getModel()->shouldSortByRelevance()) {
$searchQuery->applySortByRelevance();
}
$this->searchQuery()->setQuery($query)->search($search);
}

/**
Expand All @@ -165,6 +174,8 @@ public function searchableSortByRelevance($sortByRelevance = true)
{
$this->sortByRelevance = $sortByRelevance;

$this->searchQuery()->sortByRelevance($sortByRelevance);

return $this;
}

Expand Down
65 changes: 0 additions & 65 deletions src/SortTrait.php

This file was deleted.

0 comments on commit b85f29e

Please sign in to comment.