elasticsearch-php使用积累

基类:
<?php
/**
 * +----------------------------------------------------------
 * date: 2019/4/15 19:02
 * +----------------------------------------------------------
 * author: Raoxiaoya
 * +----------------------------------------------------------
 * describe: Elasticsearch助手类
 * +----------------------------------------------------------
 */

namespace Logic;


use Core\Singleton;

class ElasticsearchBase
{
    use Singleton;

    public $index;
    public $type;
    public $number_of_shards = 5;
    public $number_of_replicas = 1;
    public $type_source_enabled = true;
    public $properties;

    public function checkParam(){
        if(!isset($this->index) || !isset($this->type)){
            return false;
        }
    }

    public function sendResponse($code = 0, $msg = 'ok', $data = []){
        return [
            'code' => $code,
            'msg'  => $msg,
            'data' => $data
        ];
    }

    /**
     * 创建索引
     * @return bool
     */
    public function createIndex(){
        if(!$this->checkParam()){
            return false;
        }
        $params = [
            'index' => $this->index,
            'body' => [
                'settings' => [
                    'number_of_shards' => $this->number_of_shards,// 分片数
                    'number_of_replicas' => $this->number_of_replicas // 副本数
                ],
                'mappings' => [
                    $this->type => [
                        '_source' => [
                            'enabled' => $this->type_source_enabled
                        ],
                        'properties' => $this->properties
                    ]
                ]
            ]
        ];

        $client = Factory::getEsClient();
        // 索引是否存在
        if($client->indices()->exists(['index'=>[$this->index]])){
            return true;
        }

        $response = $client->indices()->create($params);
        if(is_array($response) && $response['acknowledged'] && $response['shards_acknowledged']){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 写入记录
     * @param array $body
     * @return bool
     */
    public function insertLog($body = []){
        $client = Factory::getEsClient();
        // 索引是否存在
        if(!$client->indices()->exists(['index'=>[$this->index]])){
            $this->createIndex();
        }

        $params = [
            'index' => $this->index,
            'type'  => $this->type,
            'body'  => $body
        ];

        $response = $client->index($params);
        if(is_array($response) && $response['result'] == 'created' && $response['created']){
            return true;
        }else{
            return false;
        }
    }
}

应用类:
<?php
/**
 * +----------------------------------------------------------
 * date: 2019/4/16 9:39
 * +----------------------------------------------------------
 * author: Raoxiaoya
 * +----------------------------------------------------------
 * describe: change_money_info_record记录
 * +----------------------------------------------------------
 */

namespace Logic\EsGroup;


use Core\Singleton;
use Logic\ElasticsearchBase;

class EsOfChangeMoneyRecord extends ElasticsearchBase
{
    use Singleton;

    public $index = 'leba_new';
    public $type = 'change_money_info_record';
    public $number_of_shards = 5;
    public $number_of_replicas = 1;
    public $type_source_enabled = true;
    public $properties = [
        'player_id'    => ['type' => 'integer',],
        'agent_id'     => ['type' => 'integer',],
        'partner_id'   => ['type' => 'integer',],
        'parent_id'    => ['type' => 'integer',],
        'club_room_id' => ['type' => 'integer',],
        'room_name'    => ['type' => 'string',],
        'club_room_no' => ['type' => 'integer',],
        'game_id'      => ['type' => 'integer',],
        'game_name'    => ['type' => 'string',],
        'room_id'      => ['type' => 'integer',],
        'type'         => ['type' => 'integer',],
        'tax'          => ['type' => 'integer',],
        'water'        => ['type' => 'integer',],
        'money_type'   => ['type' => 'integer',],
        'money_value'  => ['type' => 'integer',],
        'begin_value'  => ['type' => 'integer',],
        'after_value'  => ['type' => 'integer',],
        'safe_box'     => ['type' => 'integer',],
        'ext'          => ['type' => 'string',],
        'time'         => ['type' => 'integer',],
        'param'        => ['type' => 'string',],
    ];
}

使用:
$data = [
    'player_id'    => $record['change_money_player_id'],
    'agent_id'     => $promoteInfo['agent_id'],
    'partner_id'   => $promoteInfo['partner_id'],
    'parent_id'    => $promoteInfo['parent_id'],
    'club_room_id' => $record['change_money_club_room_id'],
    'room_name'    => isset($room_name) ? $room_name['club_room_name'] : '',
    'club_room_no' => $record['change_money_club_room_no'],
    'game_id'      => $record['change_money_game_id'],
    'game_name'    => $game_name ?? '',
    'room_id'      => $record['change_money_room_id'],
    'type'         => $record['change_money_type'],
    'tax'          => $record['change_money_tax'],
    'water'        => $record['change_money_water'],
    'money_type'   => $record['change_money_money_type'],
    'money_value'  => $record['change_money_money_value'],
    'begin_value'  => $record['change_money_begin_value'],
    'after_value'  => $record['change_money_after_value'],
    'safe_box'     => $player_info['player_safe_box'],
    'ext'          => $this->getTypeExt($record),
    'time'         => $record['change_money_time'],
    'param'        => is_array($record['change_money_param']) ? json_encode($record['change_money_param']) : $record['change_money_param']
];

$re = EsOfChangeMoneyRecord::getInstance()->insertLog($data);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值