borales/yii2-elasticsearch-behavior

支持 Elasticsearch 索引的 Yii2 行为

安装数: 9,383

依赖: 0

建议者: 0

安全: 0

星标: 21

关注者: 7

分支: 6

公开问题: 5

类型:yii2-extension

0.0.1 2014-12-14 22:01 UTC

This package is auto-updated.

Last update: 2024-09-18 22:36:20 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

支持 Elasticsearch 自动索引的 Yii2 AR 行为。

image

安装

通过 composer 安装此扩展是首选方式。

运行以下命令之一

$ php composer.phar require "borales/yii2-elasticsearch-behavior" "*"

或者

"borales/yii2-elasticsearch-behavior": "*"

将以下内容添加到您的 composer.json 文件的 require 部分。

配置

按如下方式配置模型(对于 "command" 模式)

class Post extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            ...
            'elasticsearch' => [
                'class' => \borales\behaviors\elasticsearch\Behavior::className(),
                'mode' => 'command',
                'elasticIndex' => 'project-index',
                'elasticType' => 'posts',
                'dataMap' => [
                    'id' => 'id',
                    'title' => 'name',
                    'body' => function() {
                        return strip_tags($this->body);
                    },
                    'date_publish' => function() {
                        return date('U', strtotime($this->date_create));
                    },
                    'author' => function() {
                        return ucfirst($this->author->name);
                    }
                ]
            ],
        ];
    }
    
    ...
}

行为配置值

  • mode(可能值:commandmodel。默认为 command)- 它是控制如何与 Elasticsearch 交互的选项
  • 如果为 command - 行为使用 \Yii::$app->elasticsearch->createCommand() 方式执行命令。在此模式下 - 需要设置 elasticIndexelasticType 参数。
  • 如果为 model - 需要设置 elasticClass 参数,其值为模型类名(指定的类必须扩展 \yii\elasticsearch\ActiveRecord 模型类)。在这种情况下,行为将通过指定的模型类与 Elasticsearch 通信。
  • dataMap - 这是一个可选参数。默认情况下 - 行为将使用 \yii\db\ActiveRecord 类的动态属性 $this->owner->attributes(您可以在此处了解如何设置此属性 这里)。否则 - 这是一个键值数组,其中键是 Elasticsearch 条目的字段名,值是当前 \yii\db\ActiveRecord 模型的字段名或匿名函数(回调)。

"model" 模式的使用示例

class Post extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            ...
            'elasticsearch' => [
                'class' => \borales\behaviors\elasticsearch\Behavior::className(),
                'mode' => 'model',
                'elasticClass' => \common\models\elastic\PostElastic::className(),
                'dataMap' => [
                    'id' => 'id',
                    'title' => 'name',
                    'body' => function() {
                        return strip_tags($this->body);
                    },
                    'date_publish' => function() {
                        return date('U', strtotime($this->date_create));
                    },
                    'author' => function() {
                        return ucfirst($this->author->name);
                    }
                ]
            ],
        ];
    }
    
    ...
}

...

class PostElastic extends \yii\elasticsearch\ActiveRecord
{
    /**
     * @return array the list of attributes for this record
     */
    public function attributes()
    {
        // path mapping for '_id' is setup to field 'id'
        return ['id', 'title', 'body', 'date_publish', 'author'];
    }
}

有关 Elasticsearch ActiveRecord 的更多详细信息和功能,请参阅这里