illuma-law/laravel-hybrid-search
Composer 安装命令:
composer require illuma-law/laravel-hybrid-search
包简介
Portable Full-Text Search (FTS) schema macros and the Reciprocal Rank Fusion (RRF) algorithm.
README 文档
README
Portable Full-Text Search macros and Reciprocal Rank Fusion for Laravel.
This package provides portable Full-Text Search (FTS) schema macros and the Reciprocal Rank Fusion (RRF) algorithm for Laravel applications. It enables seamless text searching across PostgreSQL, MySQL, SQL Server, and SQLite, abstracting away the database-specific syntax. It also provides an elegant way to merge and re-rank traditional keyword search results with AI vector search results.
Features
- Database Portability: Write one migration and one query that works across all supported databases.
- SQLite FTS5 Support: Automatically creates Virtual Tables and database triggers to keep SQLite full-text indexes synchronized.
- Reciprocal Rank Fusion: Mathematically combine multiple ranked lists (e.g., BM25 + Vector Similarity) into a single optimized result set.
- Scout Key Trait: Ensure Scout-indexed models always return a string primary key — required for Typesense and other engines that reject integer keys.
- Scout Health Check: Optional
spatie/laravel-healthcheck that probes Meilisearch, Typesense, and Algolia endpoints.
Database Support Matrix
| Database | Schema Macro | Query Builder Macro | Underlying Syntax |
|---|---|---|---|
| PostgreSQL | Native | Native | whereFullText |
| MySQL | Native | Native | whereFullText |
| SQL Server | Manual Instructions* | Native | CONTAINS |
| SQLite | Virtual Table + Triggers | Native | MATCH |
*SQL Server requires manual creation of the Full-Text Catalog.
Installation
You can install the package via composer:
composer require illuma-law/laravel-hybrid-search
The service provider will automatically register the Blueprint and Builder macros.
Usage & Integration
Schema Migrations
Use the hybridFullText macro in your migrations.
On PostgreSQL and MySQL, this directly uses Laravel's native full-text index generation. On SQLite, it creates an FTS5 virtual table (e.g., articles_fts) and sets up INSERT, UPDATE, and DELETE database triggers. This ensures your SQLite virtual table automatically stays synchronized with your main table without requiring any PHP-side application logic.
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'); } };
Full-Text Searching
Use the whereHybridFullText macro on any Query Builder or Eloquent Builder. It automatically handles the complex MATCH syntax for SQLite FTS5 and the CONTAINS syntax for SQL Server, while using native whereFullText on PostgreSQL and MySQL.
use App\Models\Article; // Search for articles containing "laravel macros" $results = Article::query() ->whereHybridFullText(['title', 'body'], 'laravel macros') ->get();
You can also invert the search to exclude matches:
// Find articles that DO NOT contain the word "outdated" $results = Article::query() ->whereHybridFullText(['title', 'body'], 'outdated', not: true) ->get();
Reciprocal Rank Fusion (RRF)
When building advanced search systems, you often want to retrieve the top results using traditional keyword search (BM25) and semantic vector search (Cosine Similarity), then combine them.
The ReciprocalRankFusion class merges these disparate result sets by assigning an RRF score to each item based on its position in the original ranked lists.
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();
Scout Key Trait
Some search engines (e.g. Typesense) require Scout keys to be strings. Add the EnsuresScoutKeyIsString trait to any model that uses integer or UUID primary keys alongside the Scout Searchable trait.
use IllumaLaw\HybridSearch\Concerns\EnsuresScoutKeyIsString; use Laravel\Scout\Searchable; class Article extends Model { use EnsuresScoutKeyIsString, Searchable { EnsuresScoutKeyIsString::getScoutKey insteadof Searchable; EnsuresScoutKeyIsString::getScoutKeyName insteadof Searchable; } }
Scout Health Check
An optional spatie/laravel-health check that pings the configured Scout engine's health endpoint. Supports Meilisearch, Typesense, and Algolia. Non-remote drivers (database, collection, null) are automatically skipped.
Install the optional dependency first:
composer require spatie/laravel-health
Then register the check in your application:
use IllumaLaw\HybridSearch\HealthChecks\ScoutEngineCheck; use Spatie\Health\Facades\Health; Health::checks([ ScoutEngineCheck::new(), ]);
The check reads standard Scout configuration keys (scout.driver, scout.meilisearch.*, scout.typesense.*, scout.algolia.*). You can adjust the request timeout via config('health.scout.timeout_seconds') (default: 5).
Testing
Run the test suite:
composer test
License
The MIT License (MIT). Please see License File for more information.
illuma-law/laravel-hybrid-search 适用场景与选型建议
illuma-law/laravel-hybrid-search 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 61 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「illuma-law」 「laravel-hybrid-search」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 illuma-law/laravel-hybrid-search 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 illuma-law/laravel-hybrid-search 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 illuma-law/laravel-hybrid-search 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
A resilient AI routing and circuit-breaker system for LLM requests in Laravel.
Standalone Cloudflare cache purging for Laravel
A configurable content safeguard and moderation pipeline for Laravel.
Focused mail health check for Spatie's Laravel Health package.
A Typesense health check for Spatie's Laravel Health package.
统计信息
- 总下载量: 61
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-20