🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
> 先安装: composer require elasticsearch/elasticsearch > 文档地址: > [elasticsearch 中文文档]([https://www.elastic.co/guide/cn/elasticsearch/php/current/index.html](https://www.elastic.co/guide/cn/elasticsearch/php/current/index.html)) [文档2]([https://es.xiaoleilu.com/030\_Data/05\_Document.html](https://es.xiaoleilu.com/030_Data/05_Document.html)) ` ~~~ <?php // +---------------------------------------------------------------------- // | Created by Vscode: JRKAdmin框架 [ JRKAdmin ] // +---------------------------------------------------------------------- // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved. // +---------------------------------------------------------------------- // | SiteUrl: http://www.luckyhhy.cn // +---------------------------------------------------------------------- // | Author: Jackhhy <jackhhy520@qq.com> // +---------------------------------------------------------------------- // | Date: 2019/12/27-8:59 // +---------------------------------------------------------------------- // | Description: // +---------------------------------------------------------------------- namespace app\index\service; use Elasticsearch\ClientBuilder; class ContentService { /** * @param array $param * @param array $keywords * @return array * @author: Jackhhy <jackhhy520@qq.com> * @name: search_common * @describe:公共搜索 */ public static function search_common( $param = [], $keywords = [],$type=2){ $client = ClientBuilder::create()->build(); //索引 $params = [ 'index' => 'ai_index', 'type' => 'ai_type', ]; $key = @implode(",", $keywords); //关键词 if ($type==2){ //主要检索 $params['body']['query']['bool']['must'][]['match']['content'] = ['query' => "$key", 'operator' => 'and']; }else{ //主要检索 $params['body']['query']['bool']['must'][]['match']['content'] = ['query' => "$key", 'operator' => 'or']; } //句子来源 if($param['structures']=="all"){ }elseif($param['structures']=='abstract'){ $params['body']['query']['bool']['must'][]['match']['tag_id'] = 1; }elseif($param['structures']=='introduction'){ $params['body']['query']['bool']['must'][]['terms'] = ['tag_id' => [2,13]]; // $params['body']['query']['bool']['must'][]['match']['tag_id'] = 2; }elseif($param['structures']=='methods'){ $params['body']['query']['bool']['must'][]['match']['tag_id'] = 3; }elseif($param['structures']=='results'){ $params['body']['query']['bool']['must'][]['match']['tag_id'] = 4; }elseif($param['structures']=='discussion'){ $params['body']['query']['bool']['must'][]['match']['tag_id'] = 5; } // IF值查询 if ((int)$param['if'] != 0) { $str = ""; switch ((int)$param['if']) { case 3: $str = ['gte' => 0, 'lt' => 3]; break; case 5: $str = ['gte' => 3, 'lt' => 5]; break; case 10: $str = ['gte' => 5, 'lt' => 10]; break; case 15: $str = ['gte' => 10, 'lt' => 100]; break; } $params['body']['query']["bool"]['must'][]["range"]['current_factor'] = $str; } /*分区*/ if ((int)$param['jcr'] != 5) { $params['body']['query']["bool"]['must'][]["match"]['types_num'] = (int)$param['jcr']; } if (isset($param['end_time']) && $param['end_time'] != 0) { $y = date("Y", time()); if ($param['end_time'] == 5) { //近五年 $f = $y - 5; $b = $f."-01-01"; $e = $y."-12-31"; } else { if ($param['end_time'] == 3) { $f = $y - 3; $b = $f."-01-01"; $e = $y."-12-31"; } else { if ($param['end_time'] == 2) { $f = $y - 2; $b = $f."-01-01"; $e = $y."-12-31"; } else { $b = $param['end_time']."-01-01"; $e = $param['end_time']."-12-31"; } } } $params['body']['query']["bool"]['must'][]["range"]['publication_time'] = [ 'from' => "$b", 'to' => "$e", ]; } if (isset($param['kai_end']) && $param['kai_end'] != 0) { $xx = @explode("_", $param['kai_end']); $b = $xx[0]."-01-01"; $e = $xx[1]."-12-31"; if ((int)$param['jcr'] == 5 && (int)$param['if'] == 0) { $params['body']['query']["bool"]['must'][]["range"]['publication_time'] = [ 'from' => "$b", 'to' => "$e", ]; } } //排序 $or = @explode(" ", $param['order']); if (strpos($param['order'], "pub_cn_rate") !== false || strpos($param['order'], "current_factor") !== false) { if (count($or) == 1) { $k = $param['order']; $params['body']['sort'] = ["$k" => ['order' => 'desc']]; } else { if (count($or) > 1) { //dump($or); $params['body']['sort'] = [$or[0] => ['order' => $or[1]]]; } } } else { if (count($or) == 1) { $params['body']['sort'] = [$param['order'] => ['order' => 'desc']]; } else { if (count($or) > 1) { $params['body']['sort'] = [$or[0]=> ['order' => $or[1]]]; } } } //dump($params); if (isset($param['limit'])) { if ((int)$param['limit'] != 0) { $params['size'] = ((int)$param['limit']-1)*20; $params['from'] = 1; } else { $params['size'] = 20; $params['from'] = ((int)$param['page']-1)*20; } } else { //多局 $params['size'] = 20; $params['from'] = 1; } //dump($params); //执行检索 $res = $client->search($params); $data = ['data' => $res['hits']['hits'], 'num' => $res['hits']['total'],'took'=>$res['took']]; // dump($data); //返回数据 return $data; } public static function get_articles($data){ $client = ClientBuilder::create()->build(); //索引 $params = [ 'index' => 'article_journals_index', 'type' => 'article_journals_type', ]; foreach ($data as $k=>$v){ $params['body']['query']['bool']['must']['bool']['should'][]['bool']=['must'=>[['term'=>['id'=>$v['_source']['aiid']]]]]; } $params['size'] = 20; $res = $client->search($params); $arr=$res['hits']['hits']; //dump($arr); $arrs=[]; if (!empty($arr)){ foreach ($arr as $k=>$v){ $arrs[$v['_source']['id']]=$v['_source']; } } // dump($arrs); return $arrs; } public static function get_duans($data){ $client = ClientBuilder::create()->build(); //索引 $params = [ 'index' => 'ai_index', 'type' => 'ai_type', ]; foreach ($data as $k=>$v){ $params['body']['query']['bool']['must']['bool']['should'][]['bool']=['must'=>[['term'=>['aiid'=>$v['_source']['aiid']]],['term'=>['tag_duan_num'=>$v['_source']['tag_duan_num']]]]]; } $params['body']['_source'] = ['aiid','content','tag_duan_num']; $params['size'] = 400; $res = $client->search($params); $arr=$res['hits']['hits']; //dump($arr); $arrs=[]; if (!empty($arr)){ foreach ($arr as $k=>$v){ $arrs[$v['_source']['aiid']."_".$v['_source']['tag_duan_num']][]=$v['_source']['content']; } } foreach($arrs as $k=>$v){ $arrs[$k]=implode(" ",$v); } return $arrs; } /** * @param $data * @return array * @author: LuckyHhy <jackhhy520@qq.com> * @date: 2020/2/24 * @name: getDuans * @describe:一次性获取全部段落 */ public static function getDuans($data){ $client = ClientBuilder::create()->build(); //索引 $params = [ 'index' => 'ai_index', 'type' => 'ai_type', ]; foreach ($data as $k=>$v){ $params['body']['query']['bool']['must']['bool']['should'][]['bool']=['must'=>[['term'=>['aiid'=>$v['article_id']]],['term'=>['tag_duan_num'=>$v['tag_duan_num']]]]]; } $params['body']['_source'] = ['aiid','content','tag_duan_num','duan_num']; $params['size'] = 400; $res = $client->search($params); $arr=$res['hits']['hits']; $arrs=[]; if (!empty($arr)){ foreach ($arr as $k=>$v){ $arrs[$v['_source']['aiid']."_".$v['_source']['tag_duan_num']][]=$v['_source']['content']; } } foreach($arrs as $k=>$v){ $arrs[$k]=implode(" ",$v); } return $arrs; } /** * @param $data * @return array * @author: LuckyHhy <jackhhy520@qq.com> * @date: 2020/2/24 * @name: getStrs * @describe: */ public static function getStrs($data){ $client = ClientBuilder::create()->build(); //索引 $params = [ 'index' => 'ai_index', 'type' => 'ai_type', ]; foreach ($data as $k=>$v){ $params['body']['query']['bool']['must']['bool']['should'][]['bool']=['must'=>[['term'=>['aiid'=>$v['article_id']]],['term'=>['tag_duan_num'=>$v['tag_duan_num']]],['term'=>['qw_num'=>$v['qw_num']]]]]; } $params['body']['_source'] = ['aiid','content','tag_duan_num','qw_num']; $params['size'] = 40; $res = $client->search($params); $arr=$res['hits']['hits']; $arrs=[]; if (!empty($arr)){ foreach ($arr as $k=>$v){ $arrs[$v['_source']['aiid']."_".$v['_source']['qw_num']][]=$v['_source']['content']; } } foreach($arrs as $k=>$v){ $arrs[$k]=implode(" ",$v); } return $arrs; } public static function Three_get($key,$param = []){ $arr=[]; foreach ($key as $k=>$v){ $f=self::get_three($v,$param); //dump($f); $arr[$k]=$f; } return $arr; } /** * @param $key * @return array * @author: Jackhhy <jackhhy520@qq.com> * @name: get_three * @describe:全文检索 */ public static function get_three($key,$param = []){ $client = ClientBuilder::create()->build(); //索引 $params = [ 'index' => 'ai_index', 'type' => 'ai_type', ]; $params['body']['query']['bool']['must'][]['bool']['should']['match']['content'] = ['query'=>"$key"]; // IF值查询 if ((int)$param['if'] != 0) { $str = ""; switch ((int)$param['if']) { case 3: $str = ['gte' => 0, 'lt' => 3]; break; case 5: $str = ['gte' => 3, 'lt' => 5]; break; case 10: $str = ['gte' => 5, 'lt' => 10]; break; case 15: $str = ['gte' => 10, 'lt' => 100]; break; } $params['body']['query']["bool"]['must'][]["range"]['current_factor'] = $str; } /*分区*/ if ((int)$param['jcr'] != 5) { $params['body']['query']["bool"]['must'][]["match"]['types_num'] = (int)$param['jcr']; //$params['body']['query']["bool"]['must'][]["match"]['types_num'] = 1; } if (isset($param['end_time']) && $param['end_time'] != 0) { $y = date("Y", time()); if ($param['end_time'] == 5) { //近五年 $f = $y - 5; $b = $f."-01-01"; $e = $y."-12-31"; } else { if ($param['end_time'] == 3) { $f = $y - 3; $b = $f."-01-01"; $e = $y."-12-31"; } else { if ($param['end_time'] == 2) { $f = $y - 2; $b = $f."-01-01"; $e = $y."-12-31"; } else { $b = $param['end_time']."-01-01"; $e = $param['end_time']."-12-31"; } } } $params['body']['query']["bool"]['must'][]["range"]['publication_time'] = [ 'from' => "$b", 'to' => "$e", ]; } if (isset($param['kai_end']) && $param['kai_end'] != 0) { $xx = @explode("_", $param['kai_end']); $b = $xx[0]."-01-01"; $e = $xx[1]."-12-31"; if ((int)$param['jcr'] == 5 && (int)$param['if'] == 0) { $params['body']['query']["bool"]['must'][]["range"]['publication_time'] = [ 'from' => "$b", 'to' => "$e", ]; } } // dump($params); //$params['body']['sort'] = ['publication_time' => ['order' => 'desc']]; $params['size'] = 5; $params['from'] = 1; //执行检索 $res = $client->search($params); $data =$res['hits']['hits']; //返回数据 return $data; } /** * @param $data * @param int $size * @return array * @author: LuckyHhy <jackhhy520@qq.com> * @date: 2020/1/6 * @name: coolect_article * @describe: */ public static function coolect_article($data,$size=20){ $client = ClientBuilder::create()->build(); //索引 $params = [ 'index' => 'article_journals_index', 'type' => 'article_journals_type', ]; foreach ($data as $k=>$v){ $params['body']['query']['bool']['must']['bool']['should'][]['bool']=['must'=>[['term'=>['id'=>$v['article_id']]]]]; } $params['size'] = $size; $res = $client->search($params); $arr=$res['hits']['hits']; //dump($arr); $arrs=[]; if (!empty($arr)){ foreach ($arr as $k=>$v){ $arrs[$v['_source']['id']]=$v['_source']; } } // dump($arrs); return $arrs; } /** * @param $duan_num * @return string * @author: Jackhhy <jackhhy520@qq.com> * @name: get_duan * @describe:获取段落 */ public static function get_duan($duan_num,$aiid){ $client = ClientBuilder::create()->build(); //索引 $params = [ 'index' => 'ai_index', 'type' => 'ai_type', ]; $params['body']['query']['bool']['must'][]['match']['tag_duan_num'] = $duan_num; $params['body']['query']['bool']['must'][]['match']['aiid'] = $aiid; $params['size'] = 60; //执行检索 $res = $client->search($params); $str=''; if (!empty($res['hits']['hits'])){ foreach ($res['hits']['hits'] as $k=>$v){ $str.=''.$v['_source']['content']; } } return $str; } } `