mabadir/elastic-laravel

此包已被弃用且不再维护。未建议替代包。

Laravel 5 的 Elastic Search 索引器

1.1.4 2017-08-24 15:08 UTC

This package is auto-updated.

Last update: 2019-09-24 14:35:33 UTC


README

Latest Version on Packagist Software License Quality Score Total Downloads

Elastic Search Indexer for Laravel 5。

结构

如果以下任何一项适用于您的项目,则目录结构应遵循行业最佳实践,并命名为以下名称。

config/
src/
tests/
vendor/

安装

通过 Composer

$ composer require mabadir/elastic-laravel

用法

ElasticLaravelServiceProvider 添加到您的 config/app.php

'providers' => [
//Other providers
    MAbadir\ElasticLaravel\ElasticLaravelServiceProvider::class,
],

发布 elastic.php 到您的配置。

$ php artisan vendor:publish

将 ElasticEloquent 特性添加到您的 Eloquent 模型以使其可索引。

namespace App;
 
use MAbadir\ElasticLaravel\ElasticEloquent;
 
class User extends Authenticatable
{
    use ElasticEloquent;
 
}

要搜索索引,您可以使用不同的方法进行搜索。第一步是将外观添加到您的 config/app.php

'aliases' => [
    //Other Facades
   'ElasticSearcher' => MAbadir\ElasticLaravel\ElasticSearcher::class,
],
  1. 简单词项搜索
ElasticSearcher::search('simple term');
  1. 在特定模型类型上进行简单词项搜索
$user = App\User::first();
ElasticSearcher::search('Simple Term', $user);

这将搜索类型为 users 的 Elastic Search 索引中的简单词项。

  1. 在特定参数上搜索索引
ElasticSearcher::search(['name' => 'First Name']);

这将搜索整个搜索索引中具有值 First Name 的参数名称。

  1. 在特定参数和特定模型类型上搜索索引
$user = App\User::first();
ElasticSearcher::search(['name' => 'First Name'], $user);

这将搜索搜索索引中具有值 First Name 的参数名称,在 type=users 上。

  1. 高级搜索
$params = [
            'body' => [
                'query' => [
                    'match' => [
                        '_all' => 'Simple Term'
                    ]
                ]
            ]
        ];
ElasticSearcher::advanced($params);

这公开了完整的 Elastic Search 强大查询 DSL 接口,这将接受任何可接受的 Elastic Search DSL 查询。

  1. 高级搜索
$user = User::first();
$params = [
            'body' => [
                'query' => [
                    'match' => [
                        '_all' => 'Simple Term'
                    ]
                ]
            ]
        ];
ElasticSearcher::advanced($params, $user);

这将使用高级查询在 type=users 上搜索索引。

对于不同的函数,可以使用类名而不是对象本身,对象或类应扩展 Eloquent 模型类 \Illuminate\Database\Eloquent\Model。例如

ElasticSearcher::search(['name' => 'First Name'], User::class);

ElasticSearch 索引控制台命令

默认情况下,该包提供了一个默认索引初始化命令

$ php artisan es:init

默认命令将以非常基本的设置初始化索引。如果需要使用更高级的设置和自定义映射来初始化索引:在您的应用程序中创建一个新的控制台命令

$ php artisan make:command IndexIntializationCommand

导入 IndexInitializationTrait 类,并重载 $params 属性

namespace App\Console\Commands;

use Illuminate\Console\Command;
use MAbadir\ElasticLaravel\Console\IndexInitializationTrait;

class IndexIntializationCommand extends Command
{
    use IndexInitializationTrait;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'es:initialize';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Initialize ElasticSearch Index';
    
    /**
     * Parameters array
     * 
     * @var array
     */
    protected $params = [
        //Custom Settings
    ];
}

有关配置参数的更多详细信息,请参阅官方 ElasticSearch 文档

变更日志

请参阅 CHANGELOG 以获取有关最近更改的更多信息。

测试

$ composer test

贡献

请参阅 CONTRIBUTINGCONDUCT 以获取详细信息。

安全

如果您发现任何安全问题,请通过电子邮件 mina@abadir.email 而不是使用问题跟踪器来报告。

致谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件