elasticsearch 工具类

<?php
/**
 * Created by PhpStorm.
 * User: brady
 * Date: 2018/4/8
 * Time: 10:31
 * esticsearch helper
 */

require APPPATH.'third_party/vendor/autoload.php';

use Elasticsearch\ClientBuilder;


class O_es
{
    protected $db_name;
    protected $tb_name;
    protected $client;
    protected $host;

    //初始化
    public function init($db_name,$tb_name)
    {

        try{
            $this->db_name = $db_name;
            $this->tb_name = $tb_name;
            $this->host = config_item('db')['host'];
            if(ENVIRONMENT !== 'development'){
                $host=['172.18.30.69:9200'];
                $this->client =  ClientBuilder::create()->setHosts($host)->build();
            } else {
                $this->client =  ClientBuilder::create()->build();
            }

            return $this->client;
        } catch(Exception $e){
            $msg = $e->getMessage();
            echo json_encode(['success'=>false,'msg'=>$msg]);exit;
        }
    }

   public function add_doc($id,$body=[])
   {
       $params = [
           'index' => $this->db_name,
           'type' => $this->tb_name,
           'id' => $id,
           'body' => $body
       ];
       $response = $this->client->index($params);
       if( isset($response['_shards']['successful']) && $response['_shards']['successful'] >0){
           return true;
       } else {
           return false;
       }
   }

    //根据id获取完整的搜索结果
    public function get_doc($id)
    {

        try{
            $params = [
                'index' => $this->db_name,
                'type' => $this->tb_name,
                'id' => (string)$id
            ];

            $response = $this->client->get($params);
            return $response;
        } catch(Exception $e){
            $msg = $e->getMessage();
            echo json_encode(['success'=>false,'msg'=>$msg]);exit;
        }

    }

    //根据id获取搜索结果
    public function get_doc_source($id)
    {
        $params = [
            'index' => $this->db_name,
            'type' => $this->tb_name,
            'id' => $id
        ];

        $response = $this->client->getSource($params);
        return $response;
    }

    //文档搜索
    public function search_index($search_index,$search_val)
    {
        try {

            $params = [
                'index' => $this->db_name,
                'type' => $this->tb_name,
                'body' => [
                    'query' => [
                        'match' => [
                            $search_index => $search_val
                        ]
                    ]
                ]
            ];

            $response = $this->client->search($params);
           if($response['hits']['total'] > 0) {
               return $response['hits']['hits'];
           } else {
               return false;//未搜索到
           }

        } catch(Exception $e){
            $msg = $e->getMessage();
            echo json_encode(['success'=>false,'msg'=>$msg]);exit;
        }
    }

    public function search_index_mul($field,$string,$type='dis_max',$tie_breaker = 0.3)
    {

        try {

            foreach($field as $v){
                $querys[] = ['match'=>[$v=>$string]];
            }


            switch($type)
            {
                case 'dis_max':
                {
                    $body = [
                        'query'=>[
                            $type=>[
                                'queries'=>[

                                ],
                                'tie_breaker'=>$tie_breaker
                            ]
                        ]
                    ];
                    $body['query'][$type]['queries'] = $querys;
                }
                break;

                case 'bool':
                {
                    $body = [
                        'query'=>[
                            $type=>[
                                'should'=>[

                                ]
                            ]
                        ]
                    ];
                    $body['query'][$type]['should'] = $querys;
                }
                    break;

                default :
                {
                    $body = [
                        'query'=>[
                            $type=>[
                                'queries'=>[

                                ]
                            ]
                        ]
                    ];
                    $body['query'][$type]['queries'] = $querys;
                }
                    break;

            }

            $params = [
                'index' => $this->db_name,
                'type' => $this->tb_name,
                'body' => $body
            ];
            $response = $this->client->search($params);
            if($response['hits']['total'] > 0) {
                return $response['hits']['hits'];
            } else {
                return [];//未搜索到
            }

        } catch(Exception $e){
            $msg = $e->getMessage();
            echo json_encode(['success'=>false,'msg'=>$msg]);exit;
        }
    }

    //文档删除
    public function del_doc($id)
    {
        try{
            $params = [
                'index' => $this->db_name,
                'type' => $this->tb_name,
                'id' => (string)$id
            ];
            $response = $this->client->delete($params);
            return $response;
        } catch(Exception $e){
            $msg = $e->getMessage();
            echo json_encode(['success'=>false,'msg'=>$msg]);exit;
        }
    }

    public function add_index()
    {
        $params = [
            'index' => $this->db_name,
            'body' => [
                'settings' => [
                    'number_of_shards' => 2,
                    'number_of_replicas' => 0
                ]
            ]
        ];

        $response = $this->client->indices()->create($params);
        print_r($response);
    }

    public function del_index()
    {
        $deleteParams = [
            'index' => $this->db_name
        ];
        $response = $this->client->indices()->delete($deleteParams);
        return $response;
    }



}

  

  $this->load->model("O_es");
        $this->load->model("Tb_articles");
        $this->O_es->init(config_item("db")['db'],'articles');

        //$res = $this->Tb_articles->get_list(1,1000,'*','desc');

//        $this->O_es->del_index();
//        foreach($res as $v){
//            $res = $this->O_es->add_doc($v['id'],$v);
//            dump("索引".$v['id'].$res);
//        }

//        $res = $this->O_es->add_doc('1',['testField' => 'hello 天安门']);
        //$res = $this->O_es->add_doc('1',['title' => 'Quick brown rabbits','body'=>'Brown rabbits are commonly seen.']);
       // $res = $this->O_es->add_doc('2',['title' => 'Keeping pets healthy','body'=>'My quick brown fox eats rabbits on a regular basis.']);
        //dump($res);
        $list = $this->O_es->search_index_mul(['title','content'],'bootstrap');
        if(!empty($list))
        {
            foreach($list as $v){
                dump($v['_source']);
            }
        }

  

posted @ 2018-04-08 17:29  brady-wang  阅读(1044)  评论(0编辑  收藏  举报