现在的代码,贴一下

<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 14-9-19
 * Time: 上午11:12
 */
namespace Application\chinalao\esengine;
use Application\Config\config;
use Elasticsearch\Client;

/**
 * 禁止直接调用本类进行ES操作
 * 要在type对应的类中继承本类
 * 并且在type类里面要初始化$this->type变量
 * Class AbstractEngine
 * @package Application\chinalao\esengine
 */
class AbstractEngine {

    protected $client;
    protected $config;
    protected $index;//索引名称
    protected $search_data;
    protected $params;

    protected function __construct(){
        $this->config = config::elasticsearch();
        $this->client = new Client($this->config['construct']);
        $this->index = $this->config['index'];
        $this->debug = config::debug();

    }
    //更改默认的index
    function setIndex($index){
        $this->index = $index;
    }

    /**
     * 返回接口操作对象,便于更自定义的操作
     * @return Client
     */
    function get_client(){
        return $this->client;
    }

    /**
     * 如果不需要返回数据,统一用这个对返回结果进行过滤
     * @param $return
     * @return bool成功返回true,失败返回false
     */
    protected function do_return($return){
        if(!isset($return['acknowledged'])){
            if($this->debug){
                return $return;
            }
            return false;
        }
        return $return['acknowledged']==1;
    }

    /**
     * 建立索引
     * @param $index
     * @param int $shards
     * @param int $replicas
     * @return bool成功返回true,失败返回false
     */
    public function createIndex($index, $shards=null, $replicas=null){
        $indexParams['index'] = $index;
        $indexParams['body']['settings']['number_of_shards'] = is_null($shards)?$this->config['number_of_shards']:$shards;
        $indexParams['body']['settings']['number_of_replicas'] = is_null($replicas)?$this->config['number_of_replicas']:$replicas;
//        $indexParams['body']['settings']['analysis'] = array(
//            'analyzer'=>array(
//                'ik'=>array(
//                    'tokenizer'=>'ik'
//                )
//            )
//        );
        $re = $this->client->indices()->create($indexParams);
        return $this->do_return($re);
    }

    /**
     * 给index设置别名
     * @param $index
     * @param $name
     * @param $body
     * @return bool成功返回true
     */
    function putAlias($index,$name,$body=array()){
        $params = array();
        $params['index'] = $index;
        $params['name'] = $name;
        $params['body'] = $body;
        $re = $this->client->indices()->putAlias($params);
        return $this->do_return($re);
    }

    /**
     * 删除别名
     * @param $index
     * @param $name
     * @return bool成功返回true
     */
    function deleteAlias($index,$name){
        $params = array();
        $params['index'] = $index;
        $params['name'] = $name;
        $re = $this->client->indices()->deleteAlias($params);
        return $this->do_return($re);
    }
    protected function putSettings($index,$body){
        $this->client->indices()->close(array('index'=>$index));
        $this->client->indices()->putSettings(array('index'=>$index,'body'=>$body));
        $this->client->indices()->open(array('index'=>$index));
        return true;
    }
    /**
     * 删除索引
     * @param $index
     * @return bool成功返回true,失败返回false
     */
    public function deleteIndex($index){
        $deleteParams['index'] = $index;
        $re =   $this->client->indices()->delete($deleteParams);
        return $this->do_return($re);
    }

    /**
     * 建立Mapping,或者给现有的type添加字段
     * @param $index
     * @param $type
     * @param array $properties_arr
     * @return bool成功返回true,失败返回false
     */
    public function createMapping($type, $mapParam = array()){
        $indexParams['index'] = $this->index;
        $indexParams['type'] = $type;
        $indexParams['body'][$indexParams['type']] = $mapParam;
        $ret = $this->client->indices()->putMapping($indexParams);
        return $this->do_return($ret);
    }

    /**
     * 删除Type表
     * @param $type
     * @return array
     */
    function deleteType($type){
        $indexParams['index']  = $this->index;
        $indexParams['type']  = $type;
        $ret = $this->client->indices()->deleteMapping($indexParams);
        return $this->do_return($ret);
    }



    /**
     * 批量添加记录
     * @param $index
     * @param $type
     * @param array $arr
     * @return bool成功返回true,失败返回false
     */
    public function insertBulk($type ,$arr = array()){
        $bulkParams['index']  = $this->index;
        $bulkParams['type']  = $type;
        $bulkParams['body']  = $arr;
        //print_r($bulkParams);die;
        $re = $this->client->bulk($bulkParams);
        //var_dump($re);die;
        if($re['errors']===false)return true;
        if($this->debug)var_dump($re);
        return false;
    }

    /**
     * 添加(更新)一条记录
     * @param $index
     * @param $type
     * @param $id
     * @param array $arr
     * @return bool成功返回true,失败返回false
     */
    public function insertOneById($type,$id,$arr = array()){
        $params = array();
        $params['index'] = $this->index;
        $params['type']  = $type;
        $params['id']    = $id;
        $params['body']  = $arr;
        $re = $this->client->index($params);
        return $this->do_return($re);
    }
    /**
     * 添加(更新)一条记录
     * @param $index
     * @param $type
     * @param array $arr
     * @return bool成功返回true,失败返回false
     */
    public function insertOne($type,$arr = array()){
        $params = array();
        $params['index'] = $this->index;
        $params['type']  = $type;
        $params['body']  = $arr;
        $re = $this->client->index($params);
//        if($re['created']===false && $this->debug){
//            return $re;
//        }
        return $re['created'];
    }


    /**
     * 获得某条记录的值
     * @param $index
     * @param $type
     * @param $id
     * @return array查询结果
     */
    public function getOne($type, $id){
        $getParams = array();
        $getParams['index'] = $this->index;
        $getParams['type']  = $type;
        $getParams['id']  = $id;
        $getParams['ignore'] = '404';
        $re = $this->client->get($getParams);
        //print_r($re);die;
        $json_re = @json_decode($re,true);
        if(!empty($json_re) && $json_re['found']===false)return null;
        return $re['_source'];
    }

    /**
     * 更新某条记录
     * @param $index
     * @param $type
     * @param $id
     * @param array $arr
     * @return bool成功返回true,失败返回false
     */
    public function  updateOne($type, $id, $arr = array()){
        $updateParams['index'] = $this->index;
        $updateParams['type']  = $type;
        $updateParams['id']    = $id;
        $updateParams['ignore'] = '404,400';
        $updateParams['body']['doc']  = $arr;
        //print_r($updateParams);die;
        $re = $this->client->update($updateParams);
        $json_re = @json_decode($re,true);
        if(!empty($json_re) ){
            if($json_re['status']===404)
                return null;
            else{//status==400的情况
                return false;
            }
        }
        //var_dump($re);die;
        return true;
    }

    /**
     * 删除某条记录
     * @param $index
     * @param $type
     * @param $id
     * @return bool成功返回true,失败返回false null为改条信息不存在
     */
    public function deleteOne($type, $id){
        $deleteParams = array();
        $deleteParams['index'] = $this->index;
        $deleteParams['type'] = $type;
        $deleteParams['id'] = $id;
        $deleteParams['ignore'] = '404,400';
        $re = $this->client->delete($deleteParams);
        $json_re = @json_decode($re,true);
        if(!empty($json_re) ){
            if($json_re['found']===false)
                return null;
            else{//status==400的情况
                return false;
            }
        }
        if($re['found']==true)return true;
        return false;
    }

    /**
     * 根据query查询语句删除记录
     * @param $type
     * @param $body
     * @return bool成功返回true,失败返回false
     */
    function delete($type,$body){
        $data = array();
        $data['index'] = $this->index;
        $data['type'] = $type;
        $data['body'] = $body;
        $re = $this->client->deleteByQuery($data);
        return $this->do_return($re);
    }

    /**
     * 统一搜索接口,
     * 其中from是搜索的结果起始位置,size是搜索结果数
     * 也可以给from参数传0,20这样的参数,类似于mysql的limit语法,会自动把20赋值给size
     * @param $type
     * @param $body
     * @param int $from可以是一个整数,也可以是“整数,整数”格式,如果是后者,size参数将不再起作用
     * @param int $size
     * @param array $sort
     * @return array 返回查询结果
     */
    function select($type,$body,$from=0,$size=10,$sort=array()){
        $params['index'] = $this->index;
        $params['type'] = $type;
        $params['body'] = $body;

        if(false !== strpos($from,',')){
            list($from,$size) = explode(',',$from);
        }
        $params['from'] = $from;
        $params['size'] = $size;
        if(!empty($sort))$params['body']['sort'] = $sort;
        //print_r($params);die;
        $this->params = $params;
        $this->search_data = $this->client->search($params);
        return $this->toArray();
    }
    function getParams(){
        return $this->params;
    }

    /**
     * 返回查询结果总数(这个结果,不受查询结果的from和size限制)
     * @return mixed
     */
    function count(){
        return isset($this->search_data['hits']['total'])?$this->search_data['hits']['total']:0;
    }

    /**
     * 直接返回es查询的结果
     * @return mixed
     */
    function getSearchData(){
        return $this->search_data;
    }

    /**
     * 将es查询结果过滤掉多余的标记,直接返回数据的二维数组
     * @param bool $complete
     * @param null $search_data
     * @return array
     */
    function toArray($complete=false,$search_data=null){
        !is_null($search_data) && $this->search_data = $search_data;
        if($complete)return $this->search_data['hits']['hits'];
        $rs = array();
        foreach($this->search_data['hits']['hits'] as $data){
            $rs[] = $data['_source'];
        }
        return $rs;
    }

    /**
     * 添加一条数据
     * @param $data
     * @return mixed
     */
    function addOne($data){
        return $this->insertOne($this->type,$data);
    }
    /**
     * 删除该mapping
     */
    function deleteMap(){
        return $this->deleteType($this->type);
    }
    /**
     * 更新一条数据,data是array(字段名=>字段值);的格式
     * 不更新的字段,不需要传递
     * @param $id
     * @param $data
     * @return mixed
     */
    function update($id,$data){
        return $this->updateOne($this->type,$id,$data);
    }

    /**
     * 删除一条记录
     * @param $id 主键
     * @return bool成功返回true
     */
    function remove($id){
        return $this->deleteOne($this->type,$id);
    }

    /**
     * 根据id获取当前type的记录
     * @param $id
     * @return array查询结果
     */
    function getDataById($id){
        return $this->getOne($this->type,$id);
    }
    /**
     * 根据多个id获取当前type的记录信息
     * @param string type 所查表
     * @param array $ids 数组
     * @param array $fields 要获取的字段,默认是所有字段
     * @param int total 如果已经知道ids的数量,就赋值给TA
     * @return array
     */
    function getDataByIds($type,array $ids,$fields = array(),$total = 0){
        $body = array(
            'query'=>array(
                'ids'=>array(
                    'values'=>$ids,
                ),
            ),
        );
        if(!empty($fields))$body['_source'] = $fields;
        if(empty($total))$total = count($ids);
        return $this->select($type,$body,0,$total);
    }

}

  

posted @ 2016-06-15 22:17  兔六哥  阅读(205)  评论(0编辑  收藏  举报