PHP code example of illuma-law / laravel-hybrid-search
1. Go to this page and download the library: Download illuma-law/laravel-hybrid-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/ */
illuma-law / laravel-hybrid-search example snippets
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
public function up(): void
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
// Creates native FTS index on pg/mysql, or FTS5 virtual table + triggers on sqlite
Schema::table('articles', function (Blueprint $table) {
$table->hybridFullText(['title', 'body'], 'articles_search_index');
});
}
public function down(): void
{
Schema::table('articles', function (Blueprint $table) {
// Safely drops native indexes or SQLite virtual tables/triggers
$table->dropHybridFullText('articles_search_index');
});
Schema::dropIfExists('articles');
}
};
use App\Models\Article;
// Search for articles containing "laravel macros"
$results = Article::query()
->whereHybridFullText(['title', 'body'], 'laravel macros')
->get();
// Find articles that DO NOT contain the word "outdated"
$results = Article::query()
->whereHybridFullText(['title', 'body'], 'outdated', not: true)
->get();
use IllumaLaw\HybridSearch\ReciprocalRankFusion;
use App\Models\Article;
// 1. Get the top 50 IDs from Keyword Search
$keywordIds = Article::query()
->whereHybridFullText(['title', 'body'], 'authentication')
->limit(50)
->pluck('id');
// 2. Get the top 50 IDs from Vector Search
$vectorIds = Article::query()
->orderByVectorSimilarity('embedding', $queryVector) // Example syntax
->limit(50)
->pluck('id');
// 3. Combine and re-rank the IDs using RRF
$rankedScores = ReciprocalRankFusion::combine(
[
'keyword' => $keywordIds,
'vector' => $vectorIds
],
k: 60 // The RRF constant (default is 60)
);
// $rankedScores is a Collection of [id => score], sorted descending by score.
$topIds = $rankedScores->keys();
// 4. Fetch the final ordered models
$finalResults = Article::whereIn('id', $topIds)
->orderByRaw('FIELD(id, ' . $topIds->implode(',') . ')')
->get();
use IllumaLaw\HybridSearch\Concerns\EnsuresScoutKeyIsString;
use Laravel\Scout\Searchable;
class Article extends Model
{
use EnsuresScoutKeyIsString, Searchable {
EnsuresScoutKeyIsString::getScoutKey insteadof Searchable;
EnsuresScoutKeyIsString::getScoutKeyName insteadof Searchable;
}
}
use IllumaLaw\HybridSearch\HealthChecks\ScoutEngineCheck;
use Spatie\Health\Facades\Health;
Health::checks([
ScoutEngineCheck::new(),
]);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.