PHP code example of nqxcode / laravel-lucene-search
1. Go to this page and download the library: Download nqxcode/laravel-lucene-search library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
public function getOptionalAttributesAttribute()
{
return [
'optional_attribute1' => 'value1',
'optional_attribute2' => 'value2',
];
}
'boost' => true
// or
'boost' => [
'accessor' => 'custom_name' // with specifying of accessor name
]
public function getBoostAttribute()
{
return 0.5; // customize boost value for model
}
namespace\FirstModel::class => [
'fields' => [
'name', 'full_description',
],
'boost' => true // enable boosting for model
],
public function getBoostAttribute()
{
return 0.5; // customize boost value for model
}
namespace\FirstModel::class => [
'fields' => [
'name', // field with default boost
'full_description' => ['boost' => 0.2], // customize boost value
],
],
public function getOptionalAttributesAttribute()
{
return [
'optional_attribute1' => 'value1', // field with default boost
'optional_attribute2' => ['boost' => 0.5, 'value' => 'value2'], // customize boost value
];
}
'analyzer' => [
'filters' => [
// Default stemming filter.
Nqxcode\Stemming\TokenFilterEnRu::class,
],
// List of paths to files with stopwords.
'stopwords' => Nqxcode\LuceneSearch\Analyzer\Stopwords\Files::get(),
],
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');
}
// ...
}
use Illuminate\Database\Eloquent\Model;
use Nqxcode\LuceneSearch\Model\SearchableInterface;
use Nqxcode\LuceneSearch\Model\SearchTrait;
class Dummy extends Model implements SearchableInterface
{
use SearchTrait;
// ...
}
Product::withoutSyncingToSearch(function () {
// mass update position for product, e.g.
foreach (Product::all() as $i => $product) {
$product->update(['position' => $i)]);
}
});
$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`.
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>'