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 = '')

//        通过  https://printlove.cn/tools/sql2es/  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'] - 1) * $param['page_size'],
            '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;
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值