EsAlbum.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <?php
  2. namespace app\controller;
  3. use think\facade\Request;
  4. use think\response\Json;
  5. /**
  6. * 期刊分析
  7. */
  8. class EsAlbum extends CommonEsController
  9. {
  10. protected $pagesize = 10;
  11. protected $albumIndex = 'album_zh';
  12. public function getAlbumIndex()
  13. {
  14. $index = $this->currentLanguage();
  15. //return $index === "document_zh" ? 'album_zh' : 'album_zh';
  16. return 'album';
  17. }
  18. /**
  19. * 期刊首页
  20. *
  21. * @param $page int 分页页码
  22. * @param $search string 搜索值
  23. * @param $search_tag string 搜索标识
  24. * @param $impactIndex string 影响指数
  25. * @return Json
  26. */
  27. public function index():Json
  28. {
  29. /**
  30. * 分页参数
  31. */
  32. $page = Request::param('page') ?? 1;
  33. $limit = ($page - 1) * $this->pagesize;
  34. /**
  35. * 搜索
  36. */
  37. $search = Request::param('search') ?? '';
  38. $searchTag = Request::param('search_tag') ?? 'album';
  39. $impactIndex = Request::param('impact_index') ?? '';
  40. $body = [];
  41. $albumIndex = $this->albumIndex;
  42. if($search || $impactIndex) {
  43. if($impactIndex) {
  44. $impactIndexArr = explode(',' , $impactIndex);
  45. $body['query']['bool']['filter'][] = [
  46. 'range' => [
  47. 'composite_impact_factor' => [
  48. 'gte' => $impactIndexArr[0] ,
  49. 'lte' => $impactIndexArr[1] ,
  50. ]
  51. ]
  52. ];
  53. }
  54. if($search) {
  55. $albumIndex = $this->getAlbumIndex();
  56. $body['query']['bool']['should'][] = [
  57. 'match_phrase' => [
  58. $searchTag => $search
  59. ]
  60. ];
  61. $body['query']['bool']['minimum_should_match'] = 1;
  62. }
  63. } else {
  64. $body['query'] = [
  65. 'match_all' => (object)[]
  66. ];
  67. }
  68. $body['from'] = $limit;
  69. $body['size'] = $this->pagesize;
  70. $body['sort'] = [
  71. [
  72. 'mark' => [
  73. 'order' => 'desc'
  74. ]
  75. ]
  76. ];
  77. $params = [
  78. 'index' => $albumIndex,
  79. 'body' => $body
  80. ];
  81. $result = $this->esCommonGetData($params);
  82. if( 0 == $result['total']) {
  83. return $this->_json_error('未查到相关数据!');
  84. }
  85. $data = $result['data'];
  86. $albumList = array_column($data, 'album');
  87. $select = 'terms';
  88. $size = $this->pagesize;
  89. foreach($data as $key => $value) {
  90. $data[$key]['tag'] = isset($value['md5']) ? 'cn' : 'en';
  91. $data[$key]['unique_val'] = isset($value['md5']) ? $value['md5'] : $value['uniq_id'];
  92. }
  93. $documentIndex = 'album_en' === $albumIndex ? 'document_en' : 'document_zh';
  94. /**
  95. * 期刊下文献统计
  96. */
  97. $paramsB = [
  98. 'index' => $documentIndex,
  99. 'body' => [
  100. 'size' => 1 ,
  101. 'query' => [
  102. 'bool' => [
  103. 'filter' => [
  104. $select => [
  105. 'album.keyword' => $albumList
  106. ]
  107. ]
  108. ]
  109. ] ,
  110. 'aggs' => [
  111. 'albumStat' => [
  112. 'terms' => [
  113. 'field' => 'album.keyword' ,
  114. 'order' => [
  115. '_count' => 'desc'
  116. ] ,
  117. 'size' => $size
  118. ] ,
  119. 'aggs' => [
  120. 'citedDocCnt' => [
  121. 'filter' => [
  122. 'range' => [
  123. 'citation_relate_count' => [
  124. 'gte' => 1
  125. ]
  126. ]
  127. ],
  128. 'aggs' => [
  129. 'citedTotalCnt' => [
  130. 'sum' => [
  131. 'field' => 'citation_relate_count'
  132. ]
  133. ]
  134. ]
  135. ] ,
  136. 'authorCnt' => [
  137. 'cardinality' => [
  138. 'field' => 'author_list.keyword'
  139. ]
  140. ] ,
  141. 'orgCnt' => [
  142. 'cardinality' => [
  143. 'field' => 'organization_parsed.keyword'
  144. ]
  145. ]
  146. ]
  147. ]
  148. ]
  149. ]
  150. ];
  151. $client = $this->getEsClient();
  152. $resultB = $client->search($paramsB);
  153. $buckets = $resultB['aggregations']['albumStat']['buckets'] ?? [];
  154. if( empty($buckets) && ! empty($data)) {
  155. return $this->_json_succ(
  156. $this->albumSearchList($data, $result['total'])
  157. );
  158. }
  159. /**
  160. * 期刊有数据 文献无数据
  161. */
  162. $newArray = [];
  163. foreach ($buckets as $key => $value) {
  164. /**
  165. * 获取期刊字段
  166. */
  167. $fields = $this->getAlbumField($data , $value['key']);
  168. /**
  169. * 名称
  170. */
  171. $newArray[$key]['album'] = $value['key'];
  172. /**
  173. * 专题名称
  174. */
  175. $newArray[$key]['special_name'] = $fields['special_name'] ?? '';
  176. /**
  177. * 所属杂志
  178. */
  179. $newArray[$key]['cn_name'] = $fields['cn_name'] ?? '';
  180. /**
  181. * 识别码
  182. */
  183. $newArray[$key]['postal_distribution_code'] = $fields['postal_distribution_code'] ?? '';
  184. $newArray[$key]['tag'] = $fields['tag'] ?? '';
  185. $newArray[$key]['unique_val'] = $fields['unique_val'] ?? '';
  186. /**
  187. * 审稿周期
  188. */
  189. $newArray[$key]['review_cycle'] = $fields['review_cycle'] ?? '';
  190. /**
  191. * 投稿命中率
  192. */
  193. $newArray[$key]['submission_hit_rate'] = $fields['submission_hit_rate'] ?? '';
  194. /**
  195. * 发文量
  196. */
  197. $newArray[$key]['doc_count'] = $value['doc_count'] ?? 0;
  198. /**
  199. * 影响指数
  200. */
  201. $newArray[$key]['composite_impact_factor'] = $fields['composite_impact_factor'] ?? 0;
  202. /**
  203. * 作者数
  204. */
  205. $newArray[$key]['author_count'] = $buckets[$key]['authorCnt']['value'] ?? 0;
  206. /**
  207. * 机构数
  208. */
  209. $newArray[$key]['org_count'] = $buckets[$key]['orgCnt']['value'] ?? 0;
  210. /**
  211. * 被引文献数量
  212. */
  213. $citedDocCnt = $buckets[$key]['citedDocCnt']['doc_count'] ?? 0;
  214. /**
  215. * 被引总量
  216. */
  217. $citedTotalCnt = $buckets[$key]['citedDocCnt']['citedTotalCnt']['value'] ?? 0;
  218. $newArray[$key]['cited_total_Cnt'] = $citedTotalCnt ?? 0;
  219. $newArray[$key]['article_Average_cited_count'] = 0 == $citedTotalCnt ? 0 : round($citedDocCnt / $citedTotalCnt , 2);
  220. }
  221. return $this->_json_succ(
  222. [
  223. 'total' => $result['total'],
  224. 'total_page' => ceil($result['total'] / $this->pagesize),
  225. 'data' => $newArray ,
  226. ]
  227. );
  228. }
  229. /**
  230. * 获取期刊字段
  231. */
  232. protected function getAlbumField($data , $album)
  233. {
  234. foreach($data as $value) {
  235. if($album === $value['album']) {
  236. return $value;
  237. }
  238. }
  239. }
  240. /**
  241. * 期刊搜索返回
  242. */
  243. protected function albumSearchList($data, $total)
  244. {
  245. foreach($data as $key => $value) {
  246. $data[$key]['doc_count'] = 0;
  247. $data[$key]['author_count'] = 0;
  248. $data[$key]['org_count'] = 0;
  249. $data[$key]['cited_total_Cnt'] = 0;
  250. $data[$key]['article_Average_cited_count'] = 0;
  251. $data[$key]['composite_impact_factor'] = $value['composite_impact_factor'] ?? '';
  252. $data[$key]['postal_distribution_code'] = $value['postal_distribution_code'] ?? '';
  253. }
  254. $count = count($data);
  255. return [
  256. 'total_page' => ceil($count / $this->pagesize) ,
  257. 'total'=>$total,
  258. 'data' => $data ,
  259. ];
  260. }
  261. /**
  262. * 期刊详情
  263. */
  264. public function albumDetail()
  265. {
  266. $tag = Request::param('tag') ?? 'tag';
  267. $value = Request::param('unique_val') ?? '';
  268. if( ! $value || ! $tag) {
  269. return $this->_json_error('请求参数有误!');
  270. }
  271. $field = 'cn' === $tag ? 'md5' : 'uniq_id';
  272. $index = 'cn' === $tag ? 'album_zh' : 'album_en';
  273. $params = [
  274. 'index' => $this->getAlbumIndex(),
  275. 'body' => [
  276. 'query' => [
  277. 'bool' => [
  278. 'filter' => [
  279. 'term' => [
  280. $field => $value
  281. ]
  282. ]
  283. ]
  284. ] ,
  285. ]
  286. ];
  287. $client = $this->getEsClient();
  288. $result = $client->search($params);
  289. $source = $result['hits']['hits'][0]['_source'];
  290. $albumCountDetail = $this->albumCountDetail($source['album'] , $tag);
  291. $albumCountDetail['album'] = $source['album'];
  292. $albumCountDetail['special_name'] = $source['special_name'] ?? '';
  293. $albumCountDetail['cn_name'] = $source['cn_name'] ?? '';
  294. $albumCountDetail['composite_impact_factor'] = $source['composite_impact_factor'] ?? '';
  295. $albumCountDetail['review_cycle'] = $source['review_cycle'] ?? '';
  296. $albumCountDetail['submission_hit_rate'] = $source['submission_hit_rate'] ?? '';
  297. return $this->_json_succ(
  298. $albumCountDetail
  299. );
  300. }
  301. /**
  302. * 期刊详情统计
  303. */
  304. protected function albumCountDetail($album , $tag)
  305. {
  306. $params = [
  307. 'index' => $this->currentLanguage() ,
  308. 'body' => [
  309. 'query' => [
  310. 'bool' => [
  311. 'filter' => [
  312. 'term' => [
  313. 'album.keyword' => $album
  314. ]
  315. ],
  316. 'must' => [
  317. [
  318. 'range' => [
  319. 'year' => [
  320. 'gte' => 2012 ,
  321. 'lte' => 2022
  322. ]
  323. ]
  324. ]
  325. ]
  326. ]
  327. ],
  328. 'aggs' => [
  329. 'citedDocCnt' => [
  330. 'filter' => [
  331. 'range' => [
  332. 'citation_relate_count' => [
  333. 'gte' => 1
  334. ]
  335. ]
  336. ] ,
  337. 'aggs' => [
  338. 'citedTotalCnt' => [
  339. 'sum' => [
  340. 'field' => 'citation_relate_count'
  341. ]
  342. ]
  343. ]
  344. ] ,
  345. 'authorCnt' => [
  346. 'cardinality' => [
  347. 'field' => 'author_list.keyword'
  348. ]
  349. ],
  350. 'orgCnt' => [
  351. 'cardinality' => [
  352. 'field' => 'organization_parsed.keyword'
  353. ]
  354. ],
  355. 'docStat' => [
  356. 'terms' => [
  357. 'field' => 'year' ,
  358. 'order' => [
  359. '_key' => 'asc'
  360. ] ,
  361. 'size' => 10
  362. ] ,
  363. 'aggs' => [
  364. 'citedPerYear' => [
  365. 'sum' => [
  366. 'field' => 'citation_relate_count'
  367. ]
  368. ]
  369. ]
  370. ],
  371. 'keywordCnt' => [
  372. 'terms' => [
  373. 'field' => 'keyword_list.keyword' ,
  374. 'order' => [
  375. '_count' => 'desc'
  376. ] ,
  377. 'size' => 20
  378. ]
  379. ]
  380. ]
  381. ]
  382. ];
  383. $client = $this->getEsClient();
  384. $result = $client->search($params);
  385. /**
  386. * 发文量
  387. */
  388. $total = $result['hits']['total'];
  389. /**
  390. * 被引量
  391. */
  392. $citedTotalCnt = $result['aggregations']['citedDocCnt']['citedTotalCnt']['value'];
  393. /**
  394. * 作者数
  395. */
  396. $authorCnt = $result['aggregations']['authorCnt']['value'];
  397. /**
  398. * 机构数
  399. */
  400. $orgCnt = $result['aggregations']['orgCnt']['value'];
  401. /**
  402. * 篇均被引量 citedDocCnt/citedTotalCnt
  403. */
  404. $citedDocCnt = $result['aggregations']['citedDocCnt']['doc_count'];
  405. if(0 == $citedTotalCnt) {
  406. $article_Average_cited_count = 0;
  407. } else {
  408. $article_Average_cited_count = round($citedDocCnt / $citedTotalCnt , 2);
  409. }
  410. $citedTrend = $result['aggregations']['docStat']['buckets'];
  411. foreach($citedTrend as $key => $value) {
  412. $newCitedTrend[$key]['key'] = $value['key'];
  413. $newCitedTrend[$key]['doc_count'] = $value['citedPerYear']['value'];
  414. }
  415. $albumCountDetail = [
  416. /**
  417. * 统计
  418. */
  419. //'total' => $total ,
  420. 'cited_total_Cnt' => $citedTotalCnt ,
  421. //'author_count' => $authorCnt ,
  422. //'org_count' => $orgCnt,
  423. //'article_Average_cited_count' => $article_Average_cited_count,
  424. /**
  425. * 统计图
  426. */
  427. 'post_trend' => $result['aggregations']['docStat']['buckets'] ,
  428. 'cited_trend' => $newCitedTrend ?? [],
  429. 'research_topic' => $result['aggregations']['keywordCnt']['buckets'] ,
  430. ];
  431. return $albumCountDetail;
  432. }
  433. /**
  434. * 根据主题获取专辑名
  435. */
  436. protected function getAlbum($specialName)
  437. {
  438. $index = $this->currentLanguage();
  439. $params = [
  440. 'index' => $index ,
  441. '_source' => 'album',
  442. 'size' => 1,
  443. 'body' => [
  444. 'query' => [
  445. 'bool' => [
  446. 'filter' => [
  447. 'term' => [
  448. 'special_name.keyword' => $specialName
  449. ]
  450. ]
  451. ]
  452. ]
  453. ]
  454. ];
  455. $client = $this->getEsClient();
  456. $result = $client->search($params);
  457. return $result['hits']['hits'][0]['_source']['album'] ?? [];
  458. }
  459. }