bvlinsky / explorer
该软件包已被废弃,不再维护。未建议替代软件包。
新一代 Elasticsearch 驱动程序,适用于 Laravel Scout。
3.4.4
2023-03-02 08:45 UTC
Requires
- php: 8.0.*||8.1.*||8.2.*
- elasticsearch/elasticsearch: ^7.16
- illuminate/support: ^9.0|^10.0
- laravel/scout: ^9.0
- webmozart/assert: ^1.10
Requires (Dev)
- infection/infection: ^0.26
- mockery/mockery: ^1.5
- phpunit/phpunit: ~9.0
- symplify/easy-coding-standard: ^9.0
README
新一代 Elasticsearch 驱动程序,适用于 Laravel Scout,具有 Elasticsearch 查询功能。
安装
通过 Composer
composer require jeroen-g/explorer
您需要配置文件来定义您的索引
php artisan vendor:publish --tag=explorer.config
同时,不要忘记遵循 Laravel Scout 安装说明,并在您的 Laravel Scout 配置中,将驱动程序设置为 elastic
。
使用方法
请务必查看 文档 以了解可执行的操作!还有一个可用的 演示应用 可能会很有帮助。
配置
您可以在配置文件中定义您索引的映射
return [ 'indexes' => [ 'posts_index' => [ 'properties' => [ 'id' => 'keyword', 'title' => 'text', ], ] ] ];
或者,您可以定义索引的模型,其余部分将为您决定
return [ 'indexes' => [ \App\Models\Post::class ], ];
在最后一种情况下,您可以实现 Explored
接口,并使用 mappableAs()
函数覆盖映射。
本质上这意味着您可以根据喜好将所有内容放在模型中,或者分别放在配置文件中。
高级查询
Laravel Scout 文档指出,“目前不支持更高级的 'where' 子句”。除了标准的模糊词搜索外,只能进行简单的 ID 检查。
$posts = Post::search('lorem ipsum')->get();
Explorer 使用查询构建器扩展了您的可能性,以便编写更复杂的查询。
例如,要获取所有
- 已发布的
- 在文档中某个地方包含 "lorem" 的
- 标题中包含 "ipsum" 的
- 可能有标签 "featured",如果有,则将其得分提高 2
您可以执行此搜索查询
$posts = Post::search('lorem') ->must(new Matching('title', 'ipsum')) ->should(new Terms('tags', ['featured'], 2)) ->filter(new Term('published', true)) ->get();
命令
请确保您已先在 config/explorer.php
中配置了索引,并运行了 Scout 命令。
搜索索引
php artisan elastic:search "App\Models\Post" lorem
变更日志
请参阅 变更日志 以了解最近的变化。
致谢
许可
MIT。有关更多信息,请参阅 许可文件。