jeroen-g/explorer

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

3.14.0 2024-04-26 08:44 UTC

README

Latest Version on Packagist

CI

新一代Laravel Scout的Elasticsearch驱动程序,具有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”的
  • 可能具有“特色”标签,如果是这样,则将其得分提高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。有关更多信息,请参阅许可证文件