# searcher **Repository Path**: fiberphp/searcher ## Basic Information - **Project Name**: searcher - **Description**: 🔍 FiberPHP 搜索组件 —— 搜索引擎抽象层,支持 Elasticsearch / MeiliSearch,统一 API,协程安全,开箱即用。 - **Primary Language**: PHP - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-08-23 - **Last Updated**: 2026-11-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # FiberPHP Searcher 搜索引擎抽象层,流式查询构造器 + 多驱动(内存 / Elasticsearch)。引擎实现 `EngineInterface`,业务按接口注入,可在内存测试与 Elasticsearch 生产间无缝切换。 ## 环境要求 - PHP >= 8.5 - Elasticsearch 驱动无额外依赖(通过 `file_get_contents` + stream context 调用 REST API) ## 安装 ```bash composer require fiberphp/searcher ``` ## 快速开始 ```php use FiberPHP\Searcher\Engine\MemoryEngine; use FiberPHP\Searcher\Index; // 内存引擎(进程内数组存储,适合测试与小型场景) $index = new Index(new MemoryEngine(), 'users'); $index->add(['id' => 1, 'name' => 'Alice', 'status' => 'active', 'age' => 30]); $index->add(['id' => 2, 'name' => 'Bob', 'status' => 'active', 'age' => 25]); // 流式查询 $users = $index->search() ->where('status', 'active') ->orderBy('age', 'desc') ->limit(10) ->get(); // 按 ID 检索 / 更新 / 删除 $user = $index->find('1'); $index->update('1', ['age' => 31]); $index->delete('1'); ``` ### Elasticsearch 引擎 ```php use FiberPHP\Searcher\Engine\ElasticsearchEngine; use FiberPHP\Searcher\Index; $engine = new ElasticsearchEngine([ 'scheme' => 'http', 'host' => '127.0.0.1', 'port' => 9200, 'index_prefix' => 'myapp_', // 索引名前缀,避免多租户冲突 'timeout' => 10, ]); $index = new Index($engine, 'users'); $index->add(['id' => 1, 'name' => 'Alice']); $users = $index->search()->where('status', 'active')->get(); ``` ## 查询构造器 `Builder` 提供流式接口,内部构建查询数组委托引擎执行: ```php $index->search() ->where('status', 'active') // 精确匹配 ->whereIn('city', ['北京', '上海']) // IN 过滤 ->search('alice') // 全文搜索(全字段匹配) ->orderBy('age', 'desc') // 排序 ->offset(0)->limit(10) // 分页 ->get(); // 执行并返回结果 $index->search()->where('status', 'active')->count(); // 计数 $index->search()->where('status', 'active')->first(); // 首条 ``` ## 特性 | 能力 | 说明 | |----------|---------------------------------------------------------------| | 引擎抽象 | `EngineInterface` 统一 index/update/delete/search/flush/count | | 多驱动 | 内存数组(测试)/ Elasticsearch REST(生产) | | 流式构造 | `Builder` 链式 where/whereIn/search/orderBy/limit/offset | | 索引封装 | `Index` 面向单索引,缺失 ID 自动生成 | | 索引前缀 | Elasticsearch 引擎 `index_prefix` 隔离多环境 | ## License MIT License (c) 2026 庞斌,详见 [LICENSE](LICENSE)。