Commit fa1c9738 authored by shajiaiming's avatar shajiaiming

es search api

parent 12959f38
......@@ -4,6 +4,7 @@ namespace api\controllers;
use Yii;
use api\base\BaseController;
use common\models\psources\LockEs;
use common\models\psources\LockCreator;
class LockController extends BaseController
......@@ -11,15 +12,19 @@ class LockController extends BaseController
public function actionLockCreator()
{
$lock_creator = new LockCreator();
$lock_creator->primaryKey = 2;//primaryKey 定义 _id
$lock_creator->primaryKey = 1;//primaryKey 定义 _id
$lock_creator->creator = 'ming';
$lock_creator->creator = 'fei';
$lock_creator->create = [
'means' => 'fix_amount',
'fix_amount' => [
'period' => 2400,
'amount' => 153500
],
'left_proportion' => [
'period' => 2400,
'tenThousandth' => 153500
],
'total_count' => 4600000,
'start_time' => 6430000,
'asset_symbol' => 'bty',
......@@ -32,37 +37,89 @@ class LockController extends BaseController
'ts' => 6632560,
'index' => 2
];
$lock_creator->action_type = 1;
$lock_creator->beneficiary = 'yi';
$lock_creator->action_type = 'create'; //create/withdraw/terminate
$lock_creator->beneficiary = 'xuan';
$lock_creator->success = true;
$lock_creator->withdraw = [
'amount' => 2000021
];
$lock_creator->terminate = [
'amount_left' => 2024121,
'amount_back' => 1536584
];
var_dump($lock_creator->save());
exit;
}
public function actionSearchLockCreator()
public function actionSearch()
{
$lock_creator = new LockCreator();
$data = null;
$msg = 'ok';
$code = 0;
$action_type = Yii::$app->request->get('action_type', '');
$beneficiary = Yii::$app->request->get('beneficiary','');
if(empty($action_type) || empty($beneficiary)){
$msg = '参数错误';
$code = -1;
goto doEnd;
}
$lock_creator = new LockEs();
$query_arr = [
"bool" => [
"must" => [
#"match" => ['creator' => '']
['match' => ['creator' => 'fei']],
['match' => ['beneficiary' => 'xuan']],
#['match' => ['unfreeze_id' => "24"]],
['match' => ['action_type' => $action_type]],
['match' => ['beneficiary' => $beneficiary]],
['match' => ['success' => true]],
]
],
];
$res = $lock_creator::find()->query($query_arr)->asArray()->all();
$resp = $lock_creator::find()->query($query_arr)->asArray()->limit(100)->all();
if(empty($resp)){
$msg = '数据为空';
$code = -1;
goto doEnd;
}
foreach ($resp as $key => $val){
$data[] = [
'block' => [
'height' => $val['_source']['block']['height'],
'ts' => $val['_source']['block']['ts'],
'hash' => $val['_source']['block']['hash'],
'index' => $val['_source']['block']['index'],
'send' => $val['_source']['block']['send'],
'txHash' => $val['_source']['block']['txHash'],
],
'creator' => $val['_source']['creator'],
'beneficiary' => $val['_source']['beneficiary'],
'unfreeze_id' => $val['_source']['unfreeze_id'],
'success' => $val['_source']['success'],
'action_type' => $val['_source']['action_type'],
'create' => [
'start_time' => $val['_source']['create']['start_time'],
'asset_exec' => $val['_source']['create']['asset_exec'],
'asset_symbol' => $val['_source']['create']['asset_symbol'],
'total_count' => $val['_source']['create']['total_count'],
'means' => $val['_source']['create']['means'],
// 'left_proportion' => [
// 'period' => $val['_source']['create']['left_proportion']['period'],
// 'tenThousandth' => $val['_source']['create']['left_proportion']['tenThousandth']
// ],
]
];
}
var_dump($res);
exit;
doEnd :
return ['code' => $code, 'data' => $data, 'msg' => $msg];;
}
public function actionDeleteCreator()
{
$lock_creator = new LockCreator();
var_dump($lock_creator->deleteIndex());
$lock_creator_v2 = new LockEs();
var_dump($lock_creator->deleteIndex(),$lock_creator_v2->deleteIndex());
exit;
}
......
<?php
namespace common\models\psources;
use yii\elasticsearch\ActiveRecord;
class LockEs extends ActiveRecord
{
// 索引名相当于库名
public static function index()
{
return 'unfreeze_tx';
}
// 类别名相当于表名
public static function type()
{
return 'unfreeze';
}
// 属性
public function attributes()
{
$mapConfig = self::mapConfig();
return array_keys($mapConfig['properties']);
}
/**
*[mapConfig mapping配置]
*返回这个模型的映射
*/
public static function mapConfig()
{
return [
'properties' => [
'creator' => ['type' => 'keyword'],
'create' => [
'type' => 'object',
'properties' => [
'means' => ['type' => 'keyword'],
'fix_amount' => [
'type' => 'object',
'properties' => [
'period' => ['type' => 'long'],
'amount' => ['type' => 'long']
]
],
'left_proportion' => [
'type' => 'object',
'properties' => [
'period' => ['type' => 'long'],
'tenThousandth' => ['type' => 'long']
]
],
'total_count' => ['type' => 'long'],
'start_time' => ['type' => 'long'],
'asset_symbol' => ['type' => 'string'],
'asset_exec' => ['type' => 'string']
]
],
'unfreeze_id' => ['type' => 'keyword'],
'block' => [
'type' => 'object',
'properties' => [
'hash' => ['type' => 'keyword'],
'height' => ['type' => 'long'],
'ts' => ['type' => 'long'],
'index' => ['type' => 'long']
]
],
'action_type' => ['type' => 'long'],
'beneficiary' => ['type' => 'keyword'],
'success' => ['type' => 'boolean'],
'withdraw' => [
'type' => 'object',
'properties' => [
'amount' => 'long'
]
],
'terminate' => [
'type' => 'object',
'properties' => [
'amount_left' => 'long',
'amount_back' => 'long'
]
]
]
];
}
public static function mapping()
{
return [
static::type() => self::mapConfig(),
];
}
/**
* 设置(更新)此模型的映射
*/
public static function updateMapping()
{
$db = self::getDb();
$command = $db->createCommand();
if (!$command->indexExists(self::index())) {
$command->createIndex(self::index());
}
$command->setMapping(self::index(), self::type(), self::mapping());
}
//获取此模型的映射
public static function getMapping()
{
$db = self::getDb();
$command = $db->createCommand();
return $command->getMapping();
}
/**
* 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; } }
return false;
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment