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/ */

    

nqxcode / laravel-lucene-search example snippets


'providers' => [
	Nqxcode\LuceneSearch\ServiceProvider::class,
],

'aliases' => [
	'Search' => Nqxcode\LuceneSearch\Facade::class,
],

'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
		]
	],

	// ...

],


        'optional_attributes' => true

        // or

        'optional_attributes' => [
                'accessor' => 'custom_name' // with specifying of accessor name
        ]

        namespace\FirstModel::class => [
                'fields' => [
                    'name', 'full_description', // fixed fields for indexing
                ],

                'optional_attributes' => true //  enable indexing for dynamic fields
        ],

        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`.

$query = Search::query('composite phrase', '*', ['proximity' => 2]);

$query = Search::query('composite phrase', '*', ['phrase' => false]);

$query = Search::rawQuery('short_description:"analog"');
// or
$rawQuery = QueryParser::parse('short_description:"analog"');
$query = Search::rawQuery($rawQuery);

$models = $query->get();

$count = $query->count();

$models = $query->limit(5, 10)->get(); // Limit = 5 and offset = 10

$paginator = $query->paginate(50);

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>'
bash
php artisan vendor:publish --provider="Nqxcode\LuceneSearch\ServiceProvider"
bash
php artisan search:rebuild --verbose
bash
php artisan search:clear