An open API service indexing awesome lists of open source software.

https://github.com/rapideinternet/laravel-searchable

Laravel search provider
https://github.com/rapideinternet/laravel-searchable

Last synced: over 1 year ago
JSON representation

Laravel search provider

Awesome Lists containing this project

README

          

# Laravel Searchable
Makes models extending the SearchableModel class searchable by indexing them using two database tables and an extensive autmatically generated word list. It uses and improved version of the Metaphone phonetic algorithm, published by Lawrence Philips in 1990. The DoubleMetaphone method tries to account for spelling errors by generating two versions of the phonetic representation of the word.
Example model
-------

``` php
20,
'content' => 5,
];

}
```

Setting up
-------
Update **composer.json** with the following entries
```
"require": {
"rapideinternet/laravel-searchable": "dev-master"
},

"repositories": [
{
"type": "vcs",
"url": "git@github.com:rapideinternet/laravel-searchable.git"
}
],
```
Update your **config\app.php**
``` php
'providers' => [
...
'Searchable\SearchableServiceProvider',
],

'aliasses' => [
...
'Search' => 'Searchable\Facades\Search',
],
```
Run the following shell commands
```
composer update
php artisan vendor:publish
php artisan migrate
```
Update your models by changing the following lines
``` php
class Website extends Model {
```
To extend the SearchableModel class instead of the Model class
``` php
class Website extends SearchableModel {
```
Add an array of searchable attributes. Each attribute carries a weight. More weight means higher relevance.
``` php
protected $searchable = [
'name' => 20,
'comments' => 5,
...
];
```
For example if searching for the word **"John"**, one item may contain 1 occurence of the query in the **'name'** attribute while another item contains 3 occurences of the query in the **'comments'** attribute.

The result would be that the first item has a score of 20 while the second item has a score of 15 and is ranked lower because the 'name' attirubte is more relevant.

Indexing
-------
Every time a model is saved or stored, it will automatically index the words present in the searchable array. This method is opportunistic and requires you to add new models through the ORM.

###Artisan
If you already have data present in your database that you need to index. You can simply run the following command:

```
php artisan searchable:index
```

###Scheduler
If you want to schedule this index you can add it to the Kernel like this.
``` php
protected $commands = [
...
'Searchable\Console\Commands\IndexCommand',
];

protected function schedule(Schedule $schedule)
{
...
$schedule->command('searchable:index')->dailyAt('4:00');
}
```