【无标题】ES 在PHP中的语法格式

2022/2/10 20:22:06

本文主要是介绍【无标题】ES 在PHP中的语法格式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  public function searchDocument($param)
    {
        
        //以下拼接是根据这条sql进行转换的

        //        [data] =>  (find_in_set(channel,'RS,TM,CISS-Normal') or universal = 1) and (type  like "%Ecommerce%" or type  like "%Telephone%") and (title like '%测试%' or content like '%测试%' or find_in_set('测试',taglist) or filelist like '%测试%') and (find_in_set('C12+RC700',model) or model = '')

//        通过  http://www.atotoa.com/ 转换了下面这条sql  or 和 and 这种php结构
//        select *from kb_faq where (a=1 or universal = 1) and (b = 2 or c=3)

        //'keys'=>$keys,'jx'=>$jx,'channel'=>$channel,'type'=>$type,$p,$page_size

//下面的拼接就是围绕着这种结构体一点点拼的

        if(!isset($param['keys']) && !isset($param['jx'])){
            return 'error 缺少必要参数!';
        }
        //分页偏移量
        if(!isset($param['p']) || $param['p'] == ''){
            $param['p'] = 0;
        }
        //分页条数
        if(!isset($param['page_size']) || $param['page_size'] == ''){
            $param['page_size'] = 30;
        }
        //es搜索条件
        $esWhere = [];
        if($param['channel']){
            $esWhere['bool']['must'][]['bool']['should'] = [['match' => ['channel' =>['query'=>$param['channel'],'analyzer'=>'ik_smart'] ]],['match' => ['universal' =>['query'=>1] ]]];
        }
        if($param['type']){
            $esWhere['bool']['must'][]['bool']['should'] = ['match' => ['type' =>['query'=>$param['type'],'analyzer'=>'ik_smart'] ]];
        }
        if($param['keys']){
            $esWhere['bool']['must'][]['bool']['should'] = [['match' => ['filelist' => ['query'=>$param['keys'],'analyzer'=>'ik_smart','minimum_should_match'=>'100%']]],['match' => ['taglist' =>['query'=>$param['keys'],'analyzer'=>'ik_smart','minimum_should_match'=>'100%'] ]],['match' => ['content' =>['query'=>$param['keys'],'analyzer'=>'ik_smart','minimum_should_match'=>'100%'] ]],['match' => ['title' =>['query'=>$param['keys'],'analyzer'=>'ik_smart','minimum_should_match'=>'100%'] ]]];
        }
        if($param['jx']){
            $esWhere['bool']['must'][]['bool']['should'] = [['match' => ['model' =>['query'=>'quan'] ]],['match' => ['model' =>['query'=>$param['jx'],'analyzer'=>'ik_smart'] ]]];
        }



// 排序
        $esOrder = ['searchnum' => 'DESC'];
        $esOrder = json_encode($esOrder);
//        $esGroup = [['groupByfaq' => ['terms'=>['field'=>'faqcode','size'=>200]]],['aggs'=>['name'=>['top_hits'=>['size'=>10,'_source'=>'name']]]]];
//        $esGroup = json_encode($esGroup);
        $esWhere = $esWhere ? json_encode($esWhere) : '';

        //es搜索  '"aggs":'.$esGroup.'
        $Where = [
            'size'  => $param['page_size'],
            'from'  => $param['p'],
            'index' => $this->index,
            'type'  => $this->type,
            'body' => '{
                "query": '.$esWhere.','.
                    '"sort": '.$esOrder.'
            }'
        ];
//        print_r($Where);die;
        $response = $this->client->search($Where);
        $hits = isset($response['hits']['hits']) ? $response['hits']['hits'] : '';
        $newArr = [];
        if($hits){
            foreach ($hits as $k => $v){
                $newArr[] = $hits[$k]['_source'];
            }
        }

        return $newArr;
    }



这篇关于【无标题】ES 在PHP中的语法格式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程