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/ */
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();
// 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.
],