yii2 elasticsearch模块代码

yii2中使用elasticsearch

Class Book extends yii\elasticsearch\ActiveRecord
{
    public static function index(){
        return "Catalog"
    }

    public static function type(){
        return "Book";
    }

    /**
     * @return array This model's mapping
     */
    public static function mapping()
    {
        return [
            static::type() => [
                'properties' => [
                    'id'             => ['type' => 'long'],
                    'name'           => ['type' => 'string'],
                    'author_name'    => ['type' => 'string', "index"=>"not_analyzed"],
                    'publisher_name' => ['type' => 'string', "index"=>"not_analyzed"],
                    'created_at'     => ['type' => 'long'],
                    'updated_at'     => ['type' => 'long'],
                    'status'         => ['type' => 'long'],
                    'suppliers'      => [
                         'type'      => 'nested',
                         'properties' => [
                               'id'  => ['type' => 'long'],
                               'name' => ['type' => 'string', 'index' => 'not_analyzed'],
                         ]
                    ]
                ]
            ],
        ];
    }

    /**
     * Set (update) mappings for this model
     */
    public static function updateMapping()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->setMapping(static::index(), static::type(), static::mapping());
    }

    /**
     * Create this model's index
     */
    public static function createIndex()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->createIndex(static::index(), [
            'settings' => [ 'index' => ['refresh_interval' => '1s'] ],
            'mappings' => static::mapping(),
            //'warmers' => [ /* ... */ ],
            //'aliases' => [ /* ... */ ],
            //'creation_date' => '...'
        ]);
    }

    /**
     * Delete this model's index
     */
    public static function deleteIndex()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->deleteIndex(static::index(), static::type());
    }

    public static function updateRecord($book_id, $columns){
       try{
            $record = self::get($book_id);
            foreach($columns as $key => $value){
                 $record->$key = $value;
            }

            return $record->update();
        }
        catch(\Exception $e){
            //handle error here
            return false;
        }
    }

    public static function deleteRecord($book_id)
    {
        try{
            $record = self::get($book_id);
            $record->delete();
            return 1;
        }
        catch(\Exception $e){
            //handle error here
            return false;
        }
    }

    public static function addRecord(Book $book){
        $isExist = false;

        try{
            $record = self::get($book->id);
            if(!$record){
                $record = new self();
                $record->setPrimaryKey($book->id);
            }
            else{
                $isExist = true;
            }
        }
        catch(\Exception $e){
            $record = new self();
            $record->setPrimaryKey($book->id);
        }

        $suppliers = [
             ['id' => '1', 'name' => 'ABC'],
             ['id' => '2', 'name' => 'XYZ'],             
        ];

        $record->id   = $book->id;
        $record->name = $book->name;
        $record->author_name = $image->author_name;
        $record->status = 1;
        $record->suppliers = $suppliers;

        try{ 
            if(!$isExist){
                $result = $record->insert();
            }
            else{
                $result = $record->update();
            }
        }
        catch(\Exception $e){
            $result = false;
            //handle error here
        }

        return $result;
    }
}

https://www.aurigait.com/how-to-use-elastic-search-in-yii2/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值