chriswillerton/searchable
Composer 安装命令:
composer require chriswillerton/searchable
包简介
Searchable trait for Laravel Eloquent models.
README 文档
README
Searchable trait for Laravel Eloquent models.
This trait uses a full text search to search individual models. A results collection is provided to help sort multiple model searches by relevance.
Installation
Add the following to the composer.json file in your project:
"chriswillerton/searchable": "1.*"
or you can run the below on the command line in the root of your project:
composer require "chriswillerton/searchable" "1.*"
Setup
To get started, add the trait to the model:
use ChrisWillerton\Searchable\Searchable;
class YourModel extends Eloquent
{
use Searchable;
protected $full_text_index = 'title, content';
You also need to define the full text index that you've setup in your database. Add this as a property on your model called $full_text_index.
Usage
You can use searchable as below:
$results = YourModel::search('search term')
->orderBy('relevance', 'desc')
->get();
The collection is used for collecting multiple model search results together and making it easy to sort the whole collection by relevance. Imagine you have an Article, Post, and Product class, but you want results from all of these mixed together and sorted by relevance. You could do something like the below:
// Set which models we wish to search
$models_to_search = [
Article::class,
Post::class,
Product::class
];
$term = 'search term';
$model_results = [];
// Loop through each model and perform the search query
foreach ($models_to_search as $model)
{
$model_results[] = $model::search($term)
->get()
->toArray();
}
// Merge each result set together into a single array of results
$merged_results = array_merge(...$model_results);
// Add these results to a collection...
$collection = new ChrisWillerton\Searchable\SearchableResults($merged_results);
// and order them by relevance
$results = $collection->getByRelevance();
You should consider the performance impact of doing the above though. It's probably best to come up with a specific search solution if maximising performance is required.
chriswillerton/searchable 适用场景与选型建议
chriswillerton/searchable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 781 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 07 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「model」 「laravel」 「eloquent」 「searchable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chriswillerton/searchable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chriswillerton/searchable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chriswillerton/searchable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package to retrieve data from Google Search Console
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
Abstraction Layer to index and search entities
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 781
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-07-06