kevinyan/laravel-elastic

Laravel 5 的 Elasticsearch 流式接口

0.0.6 2018-11-11 09:37 UTC

This package is auto-updated.

Last update: 2024-09-12 04:29:03 UTC


README

Laravel5 的 Elasticsearch 流式接口

目前只是一个非常粗糙的版本,实现了 Elasticsearch 的基本搜索功能。

安装

  • 从 Packagist 获取包
composer requrire kevinyan/laravel-elastic
  • config/app.php 中注册服务提供者和外观(在 Laravel 版本 5.5 或更高版本中,您可以跳过此步骤)
'providers' => [
    ...
    KevinYan\Elastic\Providers\ElasticServiceProvider::class,

]

'aliases' => [
    ...
    'Elastic'   => KevinYan\Elastic\Facades\Elastic::class
]

  • 将配置文件发布到项目的配置目录
php artisan vendor:publish --provider="KevinYan\Elastic\Providers\ElasticServiceProvider"

配置

我们的配置文件有很好的文档,但有一点需要强调。由于 Elasticsearch 版本 6 中删除了类型,如果您的 Elasticsearch 服务器版本高于 6,请务必不要在配置文件中设置 type 配置选项。

使用方法

// resolve elastic service from IocContainer, this will connect to the default elastic connection
$elastic = app()->make('elastic')
// connection to an specific connection
$elastic = app()->make('elastic')->connection('connection name');

// we have three different types of  term : must, should and filter, default type is must.
// search using a must term
$elastic->select(['title', 'author', 'published_at'])->term('author', 'kevin')
        ->setTimeRange('2016-01-02 12:00:00', '2016-06-05 16:23:00)->latest()
        ->get();
// search using a must term and a should term
$elastic->select(['title', 'author', 'published_at'])->term('author', 'kevin')->term('category', '10010', 'should')
        ->setTimeRange('2016-01-02 12:00:00', '2016-06-05 16:23:00)->latest()
        ->get();
                 
        
// scrolling
# The Scrolling functionality of Elasticsearch is used to paginate over many documents in a bulk manner, 
# such as exporting all the documents belonging to a single user. 
# It is more efficient than regular from + size search because it doesn’t need to maintain an expensive 
# priority queue ordering the documents.

$result = $elastic->select(['title', 'author'])->term('author', 'kevin')->get();

//this package will automatically maintain scroll_id which is used to continue paginating through the hits
while($result && count($result) > 0) {
    //loop until the scroll "cursors" are exhausted
    $result = $elastic->srcoll();
    ...
}


有时您可能需要找出 Elasticsearch 对您的搜索操作返回了什么。您可以使用 dump 方法简单地输出,此方法将所有来自 Elasticsearch 的原始返回输出到输出(浏览器或标准输出),然后结束脚本

$result = $elastic->select(['title', 'author'])->term('author', 'kevin')->dump();

使用外观

该包附带了一个外观,如果您喜欢使用静态方法调用而不是依赖注入。

在上面的示例中将 app()->make('elastic') 替换为 Elastic::

联系方式

如果您有任何问题或建议,请打开 GitHub 上的问题。

许可证

此存储库的内容是根据 MIT 许可证发布的。