jacobcyl / searchable
Eloquent 模型搜索特性。
1.7.2
2016-01-07 14:15 UTC
Requires
- php: >=5.4.0
- ext-mbstring: *
- illuminate/database: 4.2.x|~5.0
This package is not auto-updated.
Last update: 2024-09-24 20:53:29 UTC
README
base https://github.com/nicolaslopezj/searchable
由于我无法使用 composer 更新 nicolaslopezj/searchable 的分支包,我不得不在 packagist.org 上添加一个新的包。感谢 @nicolaslopezj。
Searchable,Laravel 的搜索特性
Searchable 是一个针对 Laravel 5.2 的特性,它为 Eloquent 模型添加了简单的搜索功能。
Searchable 允许你根据表和其关系中的每个字段进行搜索,并为每个字段分配优先级。
这不是针对大规模搜索优化的,但有时你只需要让它简单(尽管它并不慢)。
安装
只需将包添加到您的 composer.json
文件中,然后运行 composer update
。
"nicolaslopezj/searchable": "1.*"
"jacobcyl/searchable": "dev-master"
使用方法
将特性添加到您的模型和搜索规则中。
use Nicolaslopezj\Searchable\SearchableTrait; class User extends \Eloquent { use SearchableTrait; /** * Searchable rules. * * @var array */ protected $searchable = [ 'columns' => [ 'users.first_name' => 10, 'users.last_name' => 10, 'users.bio' => 2, 'users.email' => 5, 'posts.title' => 2, 'posts.body' => 1, ], 'joins' => [ 'posts' => ['users.id','posts.user_id'], ], ]; public function posts() { return $this->hasMany('Post'); } }
现在您可以搜索模型了。
// Simple search $users = User::search($query)->get(); // Search and get relations // It will not get the relations if you don't do this $users = User::search($query) ->with('posts') ->get();
搜索分页
与 Laravel 默认查询一样简单
// Search with relations and paginate $users = User::search($query) ->with('posts') ->paginate(20);
混合查询
搜索方法与任何 Eloquent 方法兼容。您可以这样做
// Search only active users $users = User::where('status', 'active') ->search($query) ->paginate(20);
自定义阈值
默认的接受相关性阈值是所有属性相关性的总和除以 4。要更改此值,可以在 search() 中传递第二个参数,如下所示
// Search with lower relevance threshold $users = User::where('status', 'active') ->search($query, 0) ->paginate(20);
上面的代码将按相关性顺序返回所有用户。
它是如何工作的?
Searchable 构建了一个查询,通过 Laravel 的 Eloquent 在您的模型中搜索。以下是一个示例查询
####Eloquent 模型
use Nicolaslopezj\Searchable\SearchableTrait; class User extends \Eloquent { use SearchableTrait; /** * Searchable rules. * * @var array */ protected $searchable = [ 'columns' => [ 'first_name' => 10, 'last_name' => 10, 'bio' => 2, 'email' => 5, ], ]; }
####搜索
$search = User::search('Sed neque labore', null, true)->get();
####结果
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 -- The first checks if the column is equal to the word, -- if then it adds relevance * 15 (case when first_name LIKE 'Sed' || first_name LIKE 'neque' || first_name LIKE 'labore' then 150 else 0 end) + -- The second checks if the column starts with the word, -- if then it adds relevance * 5 (case when first_name LIKE 'Sed%' || first_name LIKE 'neque%' || first_name LIKE 'labore%' then 50 else 0 end) + -- The third checks if the column contains the word, -- if then it adds relevance * 1 (case when first_name LIKE '%Sed%' || first_name LIKE '%neque%' || first_name LIKE '%labore%' then 10 else 0 end) + -- Repeats with each column (case when last_name LIKE 'Sed' || last_name LIKE 'neque' || last_name LIKE 'labore' then 150 else 0 end) + (case when last_name LIKE 'Sed%' || last_name LIKE 'neque%' || last_name LIKE 'labore%' then 50 else 0 end) + (case when last_name LIKE '%Sed%' || last_name LIKE '%neque%' || last_name LIKE '%labore%' then 10 else 0 end) + (case when bio LIKE 'Sed' || bio LIKE 'neque' || bio LIKE 'labore' then 30 else 0 end) + (case when bio LIKE 'Sed%' || bio LIKE 'neque%' || bio LIKE 'labore%' then 10 else 0 end) + (case when bio LIKE '%Sed%' || bio LIKE '%neque%' || bio LIKE '%labore%' then 2 else 0 end) + (case when email LIKE 'Sed' || email LIKE 'neque' || email LIKE 'labore' then 75 else 0 end) + (case when email LIKE 'Sed%' || email LIKE 'neque%' || email LIKE 'labore%' then 25 else 0 end) + (case when email LIKE '%Sed%' || email LIKE '%neque%' || email LIKE '%labore%' then 5 else 0 end) as relevance from `users` group by `id` -- Selects only the rows that have more than -- the sum of all attributes relevances and divided by 4 -- Ej: (20 + 5 + 2) / 4 = 6.75 having relevance > 6.75 -- Orders the results by relevance order by `relevance` desc
贡献
任何人都可以贡献。Fork,进行更改,然后提交 pull request。