archy-bold/eloquent-searchable
Composer 安装命令:
composer require archy-bold/eloquent-searchable
包简介
A service layer to provide support for search on your Eloquent models. WIP only with elasticsearch support currently.
README 文档
README
Add search functionality to Eloquent models in Laravel 5.
This package is a WIP and currently only supports very basic text search in the Java-based elasticsearch currently. I will probably add fallback search functionality in the future, but feel free to fork and add additional third-party search engines.
NOTE: This package is missing many features that means it's only useful for the most basic of searches. See the 'Issues' section for a list of known issues and functionality gaps. Improvements to come!
Requirements
PHP >= 5.4.0
This pakages uses traits which require PHP 5.4. Laravel 5 also requires PHP 5.4.
elasticsearch
The package currently only supports search through elasticsearch, so you'll need server access and to get that running before you can use this package. Download the elasticsearch server here.
Java is also required to use elasticsearch.
Installation
You can install the package through composer with the following command:
$ composer require 'archy-bold/eloquent-searchable:0.*'
Next, run composer update from your command line to get the dependencies.
Then, update config/app.php and add an entry for the service provider.
'providers' => [ // ... 'ArchyBold\EloquentSearchable\SearchServiceProvider', ];
To get the default config.php file you must run the following command:
$ php artisan vendor:publish
Setup
To make one of your models searchable, you should set it up as follows:
use ArchyBold\EloquentSearchable\SearchableModel; use ArchyBold\EloquentSearchable\SearchableTrait; class User extends Model implements SearchableModel { use SearchableTrait; protected $searchable = [ 'columns' => [ 'name', // Reference columns that should be searchable here 'country.name', // You can reference columns through relationships too. ], ];
Your model should implement SearchableModel and use SearchableTrait. You should also include a $searchable variable for configuration. Currently this only takes a columns argument. This should be an array of columns that should be indexed. You can also reference columns through relationships eg country.name.
NOTE: This only currently supports 1:1 or m:1 relationships. 1:m or m:m relationships are not currently supported.
Next add your model to the search.php config file.
'models' => [ 'users' => 'App\User', ],
Where the key is a unique name to identify the models in results.
This will allow you to search on models as follows:
$user = new User; $results = $user->search("query string");
Where the returned object is an EloquentCollection of the results.
NOTE: By default, none of your models will be indexed. They will be automatically indexed when created or updated. If you delete a model, it will be removed from the index too.
Usage
As well as searching on the model itself (See above), you can search on all searchable models too.
First get an instance of the search provider by getting it from the app layer:
$search = app('ArchyBold\EloquentSearchable\SearchProvider');
Or use dependency injection on your controllers.
use ArchyBold\EloquentSearchable\SearchProvider; class UserController extends Controller { /** * The search provider instance. */ protected $search; /** * Create a new controller instance. * * @param SearchProvider $search * @return void */ public function __construct(SearchProvider $search) { $this->search = $search; } // Or with methods injection ... public function search(SearchProvider $search) { // Use search instance } }
You then have access to an instance of a SearchProvider (in this case, always an ElasticSearchProvider). You can then call use the searchAll() function.
$results = $search->searchAll("query string"); dd($results['users']);
This returns an array of EloquentCollections, with each collection containing the results for the search on a different model. The array is indexed by the key you chose for the model in the search config.
Dealing With Errors
If you get the following exception:
CouldNotConnectToHost: couldn't connect to host
Elasticsearch isn't running. You'll need to start up the server with bin/elasticsearch where the bin/ directory is wherever you installed your elasticsearch instance.
Issues
- Initial indexing - the only models to be indexed are those added after the package has been installed.
- Basic text search only - the package currently only supports very basic searching on text.
- Limited results - only a limited number of results (10) are currently returned and pagination isn't supported.
- Limited search engines - only elasticsearch is currently supported. You need server access and Java installed to use elasticsearch.
archy-bold/eloquent-searchable 适用场景与选型建议
archy-bold/eloquent-searchable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2015 年 06 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「elasticsearch」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 archy-bold/eloquent-searchable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 archy-bold/eloquent-searchable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 archy-bold/eloquent-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.
Abstraction Layer to index and search entities
simple api library.
Pimcore 10.x Website Indexer (powered by Zend Search Lucene)
Symfony bundle for Elasticsearch integration with round-robin load balancing
统计信息
- 总下载量: 2k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-03