Elasticsearch +TP6 通过接口查询需要的数据

以下简答的根据前端传回来的数据去进行一个匹配信息 只是简单的一个小DEMO

 

<?php namespace app\api\controller;use Elasticsearch\ClientBuilder; class Tex  {     /**      * eden 文件上传接口      */     public function index()     {         // 用于存放下面不为空的数据         $data = [];         // 用来存放条件查询的数据         $chang = [];         // 1、通过企业名称搜索         $company_name = input('company_name');//接收关键字         // 以下都是判断是否为空不是空的话push 进去数组里面         if($company_name){             $datas[match] = ['company_name' => $company_name];             array_push($data,$datas);         }         // // 2、通过企业经营范围搜索         $op_scope = input('op_scope');//接收关键字         if($op_scope){             $datas[match] = ['op_scope' => $op_scope];             array_push($data,$datas);         }         // // 3、通过企业注册时间范围搜索         $es_date = input('es_date');//接收关键字         $es_dates = input(es_dates);//分为结束时间         if($es_date){             if(!$es_dates){                 $es_dates= date(Y-m-d h:i:s,time());             }             $changs[range] =[es_date=>[gt=>$es_date,lte=>$es_dates]];             array_push($chang,$changs);         }         // // 4、通过企业注册资本金范围搜索         $register_capital = input('register_capital');//接收关键字         $register_capitals = input('register_capitals');//接收关键字         if($register_capital&&$register_capitals){             $changs[range] =[zczbj=>[gt=>(float)$register_capital,lte=>(float)$register_capitals]];             array_push($chang,$changs);         }         // // 5、通过企业法人名称搜索         $legal_person = input('legal_person');//接收关键字         if($legal_person){             $datas[match] = ['legal_person' => $legal_person];             array_push($data,$datas);         }         // // 6、通过联系方式搜索         $phone = input('phone');//接收关键字         if($phone){             $datas[match] = ['phone' => $phone];             array_push($data,$datas);         }
      //如果设置了用户名密码的ES 以下的格式 如果没有的话那就是正常些IP+端口号就可以 $client = ClientBuilder::create()->setHosts(['user:[email protected]:9200'])->build();//创建es实例 //设置查询的条件 $params = [ 'index' => '库名',//索引(类似于库) 'body' => [ //查询内容 'query' => [ bool=>[ must=> $data , //上面通过push 的数组 filter=> $chang //上面push 的数组 模糊匹配的 ] ] ] ]; // return json($params); $results = $client->search($params);//es搜索 return json($results); } }

 

上面是大概的一个写法 当然也会有其他的匹配选项我们还是要自己去整理

查询出来的结果就是根据前端传回来的数据去进行查看