yii2 elasticsearch模块代码
来源:互联网 发布:淘宝上40多的牛仔裤 编辑:程序博客网 时间:2024/06/09 20:20
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 0
- yii2 elasticsearch模块代码
- yii2 elasticsearch
- 代码干货 | Elasticsearch模块——CRUD
- yii2 gii模块自动生成代码,让双手更自由
- 代码yii2
- YII2新建模块
- elasticsearch之cluster模块
- elasticsearch之gateway模块
- elasticsearch之index模块
- elasticsearch之node模块
- ElasticSearch模块Cluster
- ElasticSearch模块threadpool
- ElasticSearch模块介绍
- elasticsearch之模块【节点】
- Yii2.0框架基础--模块
- Yii2 应用结构之模块
- Yii2 中模块相关配置
- YII2.0多模块配置
- centos更改root密码
- 国内常用开源镜像网站
- 数据迁移总结
- php 命令行模式,php执行linux命令
- NodeJS优缺点及适用场景讨论
- yii2 elasticsearch模块代码
- Solr读取数据异常
- Android最佳实践——深入浅出WebSocket协议
- onResultActivity与startActivityForResult
- AR
- java 反射
- 妖刀不曾生锈
- GCD初识
- WIN7 PLSQL Developer使用oracle12c登录提示NLS_LANG未定义以及查看中文乱码问题解决