<?php
namespace app\controller;


use think\facade\Log;
use think\facade\Request;


class EsOrganization extends CommonEsController
{

    public function detail()
    {
        $organization = Request::param('organization') ?? '';
        if( ! $organization) {
            return $this->_json_error('参数请求有误！');
        }

        return $this->_json_succ(
            $this->organizationDetail($organization)
        );
    }

    /**
     * 机构分析详情
     */
    protected function organizationDetail($org)
    {
        $params = [
            'index' => $this->currentLanguage() ,
            'body'  => [
                'size' => 0,
                'query' => [
                    'bool' => [
                        'filter' => [
                            'term' => [
                                'organization_parsed.keyword' => $org
                            ]
                        ]
                    ]
                ] ,
                'aggs' => [
                    'citedTotalCnt' => [
                        'sum' => [
                            'field' => 'citation_relate_count'
                        ]
                    ] ,
                    'docStat' => [
                        'terms' => [
                            'field' => 'year' ,
                            'order' => [
                                '_key' => 'asc'
                            ] ,
                            'size' => 20
                        ] ,
                        'aggs' => [
                            'citedPerYearCnt' => [
                                'sum' => [
                                    'field' => 'citation_relate_count'
                                ]
                            ]
                        ]
                    ] ,
                    'topKeywords' => [
                        'terms' => [
                            'field' => 'keyword_list.keyword' ,
                            'order' => [
                                '_count' => 'desc'
                            ],
                            'size' => 20
                        ]
                    ] ,
                    'topAlbum' => [
                        'terms' => [
                            'field' => 'album_list.keyword' ,
                            'order' => [
                                '_count' => 'desc'
                            ] ,
                            'size' => 20
                        ]
                    ]
                ]
            ]
        ];

        $client = $this->getEsClient();
        $result = $client->search($params);

        $newCitedTrend = [];
        $citedTrend = $result['aggregations']['docStat']['buckets'];
        foreach($citedTrend as $key => $value) {
            $newCitedTrend[$key]['key']       = $value['key'];
            $newCitedTrend[$key]['doc_count'] = $value['citedPerYearCnt']['value'];
        }

        $organizationDetail = [
            'achievement_num'    => $result['hits']['total'] ,
            'citation_frequency' => $result['aggregations']['citedTotalCnt']['value'] ,
            'post_trend'         => $result['aggregations']['docStat']['buckets'] ,
            'cited_trend'        => $newCitedTrend ,
            'research_topic'     => $result['aggregations']['topKeywords']['buckets'] ,
            'top_album'          => $result['aggregations']['topAlbum']['buckets']
        ];
        return $organizationDetail;
    }

}