You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
4.2 KiB
121 lines
4.2 KiB
# element/search
|
|
|
|
[![Latest Version on Packagist](https://img.shields.io/packagist/v/element/search.svg?maxAge=2592000&style=flat-square)](https://packagist.org/packages/element/search)
|
|
[![License: AGPL v3](https://img.shields.io/badge/License-AGPL%20v3-blue.svg?style=flat-square)](LICENSE.md)
|
|
[![Author](http://img.shields.io/badge/author-Garrett_Mills-blue.svg?style=flat-square)](https://glmdev.github.io/)
|
|
[![Build Status](https://img.shields.io/travis/element/search/master.svg?style=flat-square)](https://travis-ci.org/element/search)
|
|
|
|
This is a simple hit based search engine for Eloquent models. It can take a string and will search the given set of models for each word in that string.
|
|
It prioritizes models based on the number of times each word in the search string is found in the searchable fields in the model, with more emphasis
|
|
on longer words, and less on shorter.
|
|
|
|
## Install
|
|
|
|
The search Element is available on composer. To install it, simply run:
|
|
|
|
``` bash
|
|
$ composer require element/search
|
|
```
|
|
|
|
Then, add the following line to the `$providers` array in the `config/app.php` file:
|
|
|
|
``` php
|
|
Element\Search\SearchServiceProvider::class,
|
|
```
|
|
|
|
|
|
## Use
|
|
|
|
Making your models searchable is easy. The models simply need to implement the `Element\Search\SearchableContract`
|
|
interface. Then, add the `getSearchable()` method to the model. This method should return an array of the names
|
|
of the Model's fields that the search engine can search. These fields should hold string/text data only.
|
|
|
|
For Example:
|
|
|
|
``` php
|
|
<?php
|
|
|
|
namespace App;
|
|
|
|
use Element\Search\SearchableContract;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class BlogPost extends Model implements SearchableContract {
|
|
protected $fillable = [ 'date-created', 'date-published', 'title', 'body',
|
|
'author', 'image', 'viewcount' ];
|
|
|
|
public static function getSearchable(){
|
|
return [ 'title', 'body', 'author' ];
|
|
}
|
|
|
|
public static function search( $string ){
|
|
return Search::search( new self, $string );
|
|
}
|
|
}
|
|
|
|
```
|
|
|
|
In the model above, we can see from the `getSearchable()` function that the Search Engine will search
|
|
the 'title', 'body', and 'author' fields.
|
|
|
|
The second function is optional, but it allows you to call the search function statically from the
|
|
model, rather than the search facade.
|
|
|
|
ie:
|
|
``` php
|
|
App\BlogPost::search('search string');
|
|
```
|
|
|
|
### The Search Facade
|
|
|
|
The Element Search plugin adds a Search facade to the Laravel app. This allows you to call a
|
|
search on any model from anywhere using the following function:
|
|
|
|
``` php
|
|
Search::search( new App\ModelName, 'search string');
|
|
```
|
|
|
|
Where `App\ModelName` is a new instance of the model you wish to search, and `'search string'` is
|
|
any string you wish to search for. Additionally, you don't have to worry about removing punctuation
|
|
or capital letters from your search string, the function does it automatically.
|
|
|
|
The search function, whether in the model, or called from the facade, returns the models in the form
|
|
of a `\Laravel\Database\Eloquent\Collection` collection. This allows you to perform all the usual
|
|
functions you would be able to perform on a database query.
|
|
|
|
|
|
## Contributing
|
|
|
|
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
|
|
|
|
## Security
|
|
|
|
If you discover any security related issues, please email glmdev@outlook.com instead of using the issue tracker.
|
|
|
|
## Credits
|
|
|
|
- [Garrett Mills](https://glmdev.github.io/)
|
|
- [All Contributors](CONTRIBUTORS)
|
|
|
|
## License
|
|
|
|
The search Element is licensed under the GNU Affero General Public
|
|
License, version 3.0.
|
|
|
|
```
|
|
element/search
|
|
Copyright (C) 2016 Garrett Mills
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
``` |