2017 © Pedro Peláez
 

library elastic-scout

Elastic Driver for Laravel Scout

image

shrimpliu/elastic-scout

Elastic Driver for Laravel Scout

  • Monday, February 12, 2018
  • by shrimpliu
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Laravel ElasticSearch Scout引擎

  • 支持自定义映射字段
  • 支持ES suggest方法
  • 支持随机排序
  • 支持ES搜索语法

安装

使用composer安装包, (*1)

composer require shrimpliu/elastic-scout

添加provider到config/app.php配置中(Laravel 5.4及以下版本需要), (*2)

'providers' => [

    ...

    ShrimpLiu\ElasticScout\ElasticScoutServiceProvider::class,
]

配置

配置索引

默认索引与模型表名相同,也可以通过覆盖searchableAs方法来自定义。, (*3)

<?php
use ShrimpLiu\ElasticScout\Traits\ElasticSearchable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use ElasticSearchable;

    /**
     * Get the index name for the model.
     *
     * @return string
     */
    public function searchableAs()
    {
        return 'posts_index';
    }
}

配置可搜索数据

默认,索引会从模型的toArray方法来读取数据,可以覆盖toSearchableArray方法来自定义索引数据。, (*4)

<?php
use ShrimpLiu\ElasticScout\Traits\ElasticSearchable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use ElasticSearchable;

    /**
     * 自定义索引数据
     *
     * @return array
     */
    public function toSearchableArray()
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'author_id' => $this->author_id,
            'category_id' => $this->category_id,
            'author_name' => $this->author->name,
            'content' => $this->content,
            'is_publish' => (boolean)$this->is_publish,
            'insert_time' => $this->insert_time,
            'update_time' => $this->update_time
        ];
    }

    public function author()
    {
        return $this->belongsTo('App\Author', 'author_id');
    }
}

自定义索引字段类型

默认,同步到索引时,会根据数据自动选择字段类型,可以覆盖customSearchProperties方法来自定义字段类型。, (*5)

<?php
use ShrimpLiu\ElasticScout\Traits\ElasticSearchable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use ElasticSearchable;

    /**
     * 自定义索引字段类型
     *
     * @return array
     */
    public function toSearchableArray()
    {
        return [
            'id' => ['type' => 'integer'],
            'author_id' => ['type' => 'integer'],
            'category_id' => ['type' => 'integer'],
            'author_name' => ['type' => 'string', 'index' => 'not_analyzed'],
            'is_publish' => ['type' => 'boolean'],
            'insert_time' => ['type' => 'date', 'format' => 'epoch_millis'],
            'update_time' => ['type' => 'date', 'format' => 'epoch_millis']
        ];
    }
}

有关ElasticSearch的字段数据类型可以参考ES官方文档, (*6)

索引

字段映射

如果有自定义索引字段类型,在导入数据之前,需先映射字段到索引中,运行map命令:, (*7)

php artisan elastic:map "App\Post"

批量导入

php artisan scout:import "App\Post"

批量删除

php artisan scout:flush "App\Post"

使用

搜索

$posts = App\Post::search('php laravel')->get();

筛选

$posts = App\Post::search('php laravel')->filter([
    'bool' => [
        'must' => [
            ['term' => ['category_id' => 233]],
            ['term' => ['is_publish' => true]]
        ],
        'must_not' => [
            ['term' => ['id' => 234]]
        ]
    ]
])->get();

随机排序

$posts = App\Post::search('php laravel')->inRandomOrder()->get();

suggest方法

$posts = App\Post::suggest("title", "Laravel实现ES Scout驱动")->get();

The Versions

12/02 2018

dev-master

9999999-dev

Elastic Driver for Laravel Scout

  Sources   Download

The Requires

 

The Development Requires

laravel elasticsearch elastic scout

12/02 2018

1.0.0.x-dev

1.0.0.9999999-dev

Elastic Driver for Laravel Scout

  Sources   Download

The Requires

 

The Development Requires

laravel elasticsearch elastic scout

12/02 2018

1.0.0

1.0.0.0

Elastic Driver for Laravel Scout

  Sources   Download

The Requires

 

The Development Requires

laravel elasticsearch elastic scout