marmark0/explorer

Laravel Scout 的下一代 Elasticsearch 驱动程序。

v3.12.1 2024-03-15 09:52 UTC

This package is auto-updated.

Last update: 2024-09-15 11:11:51 UTC


README

Latest Version on Packagist

CI

具有 Elasticsearch 查询功能的 Laravel Scout 的下一代 Elasticsearch 驱动程序。

安装

通过 Composer

composer require marmark0/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。请参阅 许可证文件 了解更多信息。