elasticsearch进行and,or多条件组合查询
其中的条件类似于mysql的where cate_id=$cate_id and (title=$keyword or content=$keyword)
$keyword = '常德学院'; $cate_id = 148; $url = 'http://127.0.0.1/aaaaaaa/_search'; if($ent_id){ $data['query']['bool']['filter'][0]['bool']['must']['match_phrase']['cate_id'] = $cate_id; } $data['query']['bool']['filter'][1]['bool']['should'][0]['match_phrase']['title'] = $keyword; $data['query']['bool']['filter'][1]['bool']['should'][1]['match_phrase']['content'] = $keyword; $data['from'] = 0; $data['size'] = 10; $data['sort'] = []; //下面是搜索关键词高亮设置 $a = json_decode('{}'); $data['highlight']['fields'][0]['hw_doc_title'] = $a; $data['highlight']['fields'][1]['hw_doc_body'] = $a; $data['highlight']['pre_tags'] = '<font color="red">'; $data['highlight']['post_tags'] = '</font>'; $data['highlight']['fragment_size'] = 20; $post_data = json_encode($data,JSON_UNESCAPED_UNICODE); $relust = curl_post($url,$post_data); print_r($relust);
function curl_post($url,$data) { $curl = curl_init(); curl_setopt($curl,CURLOPT_URL,$url); curl_setopt($curl, CURLOPT_HTTPHEADER,array( 'Content-Type: application/json; charset=utf-8', 'Cache-Control: no-cache', 'Pragma: no-cache' )); curl_setopt($curl,CURLOPT_RETURNTRANSFER,true); curl_setopt($curl,CURLOPT_POST,true); curl_setopt($curl,CURLOPT_POSTFIELDS,$data); $result = curl_exec($curl); curl_close($curl); $datalist = json_decode($result,true); return $datalist; }