PHP code example of aghfatehi / laravel-smartsearch

1. Go to this page and download the library: Download aghfatehi/laravel-smartsearch 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/ */

    

aghfatehi / laravel-smartsearch example snippets


SmartSearch::for(Product::class)->query('phone')->where('price', '>', 100)->get();

use SmartSearch\Traits\SmartSearchable;

class Product extends Model
{
    use SmartSearchable;

    protected $smartSearchable = ['name', 'description', 'price'];
}

Schema::table('products', function ($table) {
    $table->text('embedding')->nullable();
});



use SmartSearch\Traits\SmartSearchable;

class Property extends Model
{
    use SmartSearchable;

    protected array $smartSearchable = ['title', 'description', 'city'];

    public function searchableEmbeddings(): array
    {
        return ['title', 'description', 'city', 'neighborhood'];
    }
}

// Full-text + semantic combined (hybrid)
SmartSearch::for(Property::class)
    ->query('شقة في الرياض')
    ->similarTo('قريبة من المدارس والخدمات')  // ← semantic search
    ->where('price', '<', 1000000)
    ->get();

// 30% full-text, 70% semantic
->similarTo('قريبة من المدارس', weight: 0.3)

use Laravel\Scout\Searchable;

class Product extends Model
{
    use Searchable;
}

use SmartSearch\Facades\SmartSearch;

$results = SmartSearch::for(Product::class)
    ->query('iphone')
    ->get();

$results = SmartSearch::for(Product::class)
    ->query('iphone')
    ->where('price', '<', 5000)
    ->where('brand', 'Apple')
    ->get();

$results = SmartSearch::for(Product::class)
    ->query('laptop')
    ->paginate(20);

Product::smartSearch('iphone')->get();

smartSearch(Product::class, 'iphone')->get();

// No special error handling needed
$results = SmartSearch::for(Product::class)->query('iphone')->get();
// Automatically falls back to database search

use SmartSearch\Support\ArabicNormalizer;

ArabicNormalizer::normalize('مُحَمَّد'); // محمد
ArabicNormalizer::normalize('أحمد إبراهيم آدم'); // احمد ابراهيم ادم

// config/smartsearch.php
'elasticsearch' => [
    'analyzer' => 'arabic', // or 'standard', 'english', custom, etc.
],

// config/smartsearch.php
return [
    'driver' => env('SMARTSEARCH_DRIVER', 'elasticsearch'),
    'fallback' => env('SMARTSEARCH_FALLBACK', 'database'),
    'queue' => env('SMARTSEARCH_QUEUE', true),
    'connection' => env('SMARTSEARCH_CONNECTION', 'default'),
    'elasticsearch' => [
        'hosts' => explode(',', env('ELASTICSEARCH_HOSTS', 'localhost:9200')),
        'analyzer' => env('ELASTICSEARCH_ANALYZER', 'standard'),
    ],
    'index_prefix' => env('SMARTSEARCH_INDEX_PREFIX', ''),
];
bash
php artisan vendor:publish --tag=smartsearch-config
bash
composer 

SMARTSEARCH_EMBEDDINGS_ENABLED=true
SMARTSEARCH_EMBEDDINGS_PROVIDER=ollama
SMARTSEARCH_EMBEDDINGS_MODEL=nomic-embed-text
SMARTSEARCH_EMBEDDINGS_HOST=http://localhost:11434