Package Data | |
---|---|
Maintainer Username: | nqxcode |
Maintainer Contact: | nc101ux@gmail.com (Andrey Pokoev) |
Package Create Date: | 2014-07-08 |
Package Last Update: | 2020-03-29 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-19 03:24:35 |
Package Statistics | |
---|---|
Total Downloads: | 16,458 |
Monthly Downloads: | 2 |
Daily Downloads: | 0 |
Total Stars: | 73 |
Total Watchers: | 4 |
Total Forks: | 28 |
Total Open Issues: | 5 |
Laravel 5.5 package for full-text search over Eloquent models based on ZendSearch Lucene.
Require this package in your composer.json and run composer update:
{
"require": {
"nqxcode/laravel-lucene-search": "2.4.*"
}
}
After updating composer, add the ServiceProvider to the providers array in config/app.php
'providers' => [
Nqxcode\LuceneSearch\ServiceProvider::class,
],
If you want to use the facade to search, add this to your facades in config/app.php
:
'aliases' => [
'Search' => Nqxcode\LuceneSearch\Facade::class,
],
Publish the config file into your project by running:
php artisan vendor:publish --provider="Nqxcode\LuceneSearch\ServiceProvider"
In published config file add descriptions for models which need to be indexed, for example:
'index' => [
// ...
namespace\FirstModel::class => [
'fields' => [
'name', 'full_description', // fields for indexing
]
],
namespace\SecondModel::class => [
'fields' => [
'name', 'short_description', // fields for indexing
]
],
namespace\ModelWithCustomPrimaryKey::class => [
// You can also define your primary key (if you use something else than "id")
'primary_key' => 'my_custom_field_name',
'fields' => [
'username', 'short_description', // fields for indexing
]
],
// ...
],
You can also index values of optional fields (dynamic fields). For enable indexing for optional fields:
'optional_attributes' => true
// or
'optional_attributes' => [
'accessor' => 'custom_name' // with specifying of accessor name
]
field-name => field-value
.
By default getOptionalAttributesAttribute
accessor will be used.
In case accessor name specified in config getCustomNameAttribute
accessor will be used.Example:
In config file:
namespace\FirstModel::class => [
'fields' => [
'name', 'full_description', // fixed fields for indexing
],
'optional_attributes' => true // enable indexing for dynamic fields
],
In model add following accessor:
public function getOptionalAttributesAttribute()
{
return [
'optional_attribute1' => 'value1',
'optional_attribute2' => 'value2',
];
}
See details on Apache Lucene - Scoring.
This is Document level boosting in terminology of Apache Lucene. By default all models have boost value equal to 1. For change of this behavior customize boost for necessary models as in the following examples.
'boost' => true
// or
'boost' => [
'accessor' => 'custom_name' // with specifying of accessor name
]
In model add following accessor:
public function getBoostAttribute()
{
return 0.5; // customize boost value for model
}
getBoostAttribute
accessor will be used.
In case accessor name specified in config getCustomNameAttribute
accessor will be used.Example:
In config file:
namespace\FirstModel::class => [
'fields' => [
'name', 'full_description',
],
'boost' => true // enable boosting for model
],
In model add following accessor:
public function getBoostAttribute()
{
return 0.5; // customize boost value for model
}
This is Document's Field level boosting in terminology of Apache Lucene. By default boost is set in 1 for each field. For change of this behavior set boost for necessary fields as in the following examples.
In config file:
namespace\FirstModel::class => [
'fields' => [
'name', // field with default boost
'full_description' => ['boost' => 0.2], // customize boost value
],
],
Or/and in model accessor:
public function getOptionalAttributesAttribute()
{
return [
'optional_attribute1' => 'value1', // field with default boost
'optional_attribute2' => ['boost' => 0.5, 'value' => 'value2'], // customize boost value
];
}
By default the following filters are used in search:
This filters can be deleted or replaced with others.
'analyzer' => [
'filters' => [
// Default stemming filter.
Nqxcode\Stemming\TokenFilterEnRu::class,
],
// List of paths to files with stopwords.
'stopwords' => Nqxcode\LuceneSearch\Analyzer\Stopwords\Files::get(),
],
For building of search index run:
php artisan search:rebuild --verbose
For clearing of search index run:
php artisan search:clear
For filtering of models in search results each model's class can implements SearchableInterface
.
For example:
use Illuminate\Database\Eloquent\Model;
use Nqxcode\LuceneSearch\Model\SearchableInterface;
class Dummy extends Model implements SearchableInterface
{
// ...
/**
* Get id list for all searchable models.
*/
public static function searchableIds()
{
return self::wherePublish(true)->pluck('id');
}
// ...
}
For register of necessary events (save/update/delete) use Nqxcode\LuceneSearch\Model\SearchTrait
in target model:
use Illuminate\Database\Eloquent\Model;
use Nqxcode\LuceneSearch\Model\SearchableInterface;
use Nqxcode\LuceneSearch\Model\SearchTrait;
class Dummy extends Model implements SearchableInterface
{
use SearchTrait;
// ...
}
If you want to avoid triggering the indexing, wrap necessary operations in the withoutSyncingToSearch()
method on your model:
Product::withoutSyncingToSearch(function () {
// mass update position for product, e.g.
foreach (Product::all() as $i => $product) {
$product->update(['position' => $i)]);
}
});
Build query in several ways:
By default, queries which will execute search in the phrase entirely are created.
$query = Search::query('clock'); // search by all fields.
// or
$query = Search::where('name', 'clock'); // search by 'name' field.
// or
$query = Search::query('clock') // search by all fields with
->where('short_description', 'analog'); // filter by 'short_description' field.
// or
$query = Product::search('clock'); // search only in `Product` model by all fields in case when `Product` use `SearchableTrait`.
For query
and where
methods it is possible to set the following options:
Find all models in which any field contains phrase like 'composite one two phrase':
$query = Search::query('composite phrase', '*', ['proximity' => 2]);
Search by each word in query:
$query = Search::query('composite phrase', '*', ['phrase' => false]);
$query = Search::rawQuery('short_description:"analog"');
// or
$rawQuery = QueryParser::parse('short_description:"analog"');
$query = Search::rawQuery($rawQuery);
For built query are available following actions:
$models = $query->get();
$count = $query->count();
$models = $query->limit(5, 10)->get(); // Limit = 5 and offset = 10
$paginator = $query->paginate(50);
Highlighting of matches is available for any html fragment encoded in utf-8 and is executed only for the last executed request.
Search::find('nearly all words must be highlighted')->get();
$highlighted = Search::highlight('all words');
// highlighted html:
// '<span class="highlight">all</span> <span class="highlight">words</span>'
Package licenced under the MIT license.