bittenbyte/laravel-scout-elasticsearch

为Laravel Scout定制的ElasticSearch引擎

dev-master / 1.0.x-dev 2017-03-07 14:11 UTC

This package is not auto-updated.

Last update: 2024-09-18 19:59:29 UTC


README

为Laravel Scout(3.*)实现ElasticSearch定制引擎

重要 这仍然是一个实验性包!!!

安装

通过composer安装此包

composer require bittenbyte/laravel-scout-elasticsearch

将服务提供者添加到config/app.php文件中的providers部分(当然,您还需要Scout)

    ...
    BittenByte\ScoutElasticsearchEngine\ScoutElasticsearchEngineServiceProvider::class,
    ...

在config/scout.php中设置正确的驱动程序,并添加如下键

    ...
    'driver' => 'elasticsearch',
    ...
    'elasticsearch' => [
        'config' => [
            'hosts' => array_map('trim', explode(',', env('ELASTICSEARCH_HOSTS', 'localhost'))),
        ],
        //default searchable fields per index
        'fields' => [
            'users_index' => [
                'name', 'email', 'role', 'slug',
            ],
        ],
        'indices' => [
            //set your OPTIONAL index specific settings and mappings
            'users_index' => [
                'settings' => [
                    'number_of_shards' => 3,
                    'number_of_replicas' => 2
                ],
                'mappings' => [
                    'authenticable' => [
                        '_source' => [
                            'enabled' => true,
                        ],
                        'properties' => [
                            'name' => [
                                'type' => 'string',
                                'index' => 'not_analyzed',
                            ],
                            'slug' => [
                                'type' => 'string',
                                'index' => 'not_analyzed',
                            ],
                            'role' => [
                                'type' => 'string',
                                'index' => 'not_analyzed',
                            ],
                            'email' => [
                                'type' => 'string',
                                'index' => 'not_analyzed',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],

用法

遵循以下文档和Scout的官方文档

待办事项(文档)

高级/自定义查询构建器

文档

别忘了从Scout文档中获取

索引化自动链接到Eloquent的创建、更新和(软)删除操作

也可以手动索引/从索引中删除

// Updating via Eloquent query...
App\Order::where('price', '>', 100)->searchable();

// You may also update via relationships...
$user->orders()->searchable();

// You may also update via collections...
$orders->searchable();
// Removing via Eloquent query...
App\Order::where('price', '>', 100)->unsearchable();

// You may also remove via relationships...
$user->orders()->unsearchable();

// You may also remove via collections...
$orders->unsearchable();

暂停索引化

这在进行批量操作时特别有用

App\Order::withoutSyncingToSearch(function () {
    // Perform model actions...
});

搜索(在tinker中尝试)

$orders = App\Order::search('Star Trek')->get();

Where子句(只有id)。奇怪的概念(NoSQL和SQL的where子句?)

$orders = App\Order::search('Star Trek')->where('user_id', 1)->get();

分页

$orders = App\Order::search('Star Trek')->paginate();