server/src/app/Http/Controllers/Api/GeoStatsController.php
author ymh <ymh.work@gmail.com>
Wed, 09 Nov 2016 15:05:41 +0100
changeset 406 cf0f23803a53
parent 378 5b47eab083f3
child 407 2dba812c7ef2
permissions -rw-r--r--
upgrade elasticsearch to 5.0, upgrade ember
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
namespace CorpusParole\Http\Controllers\Api;
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
use Illuminate\Http\Request;
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
use CorpusParole\Http\Controllers\Controller;
378
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
     8
use CorpusParole\Libraries\Filters\CorpusFilterManager;
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
     9
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
use Es;
320
0fce13da58af filter geostat by area + tests
ymh <ymh.work@gmail.com>
parents: 308
diff changeset
    11
use Log;
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
class GeoStatsController extends Controller
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
{
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    /**
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
     * Display the specified resource.
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
     *
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
     * @return \Illuminate\Http\Response
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
     */
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    public function index(Request $request)
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    {
320
0fce13da58af filter geostat by area + tests
ymh <ymh.work@gmail.com>
parents: 308
diff changeset
    22
        $area = $request->input('area');
378
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    23
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    24
        $filterManager = new CorpusFilterManager();
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    25
        $filters = $filterManager->prepareFilters($request);
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    26
        unset($filters['location']);
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    27
        $qFilterParts = $filterManager->buildESFilters($filters);
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    28
        if(is_null($qFilterParts)) {
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    29
            $qFilterParts = [];
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    30
        }
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    31
320
0fce13da58af filter geostat by area + tests
ymh <ymh.work@gmail.com>
parents: 308
diff changeset
    32
        if(!is_null($area) && $area !== config('corpusparole.geonames_earth_geonamesid')) {
378
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    33
            $qFilterParts[] = [
320
0fce13da58af filter geostat by area + tests
ymh <ymh.work@gmail.com>
parents: 308
diff changeset
    34
                'term' => [
0fce13da58af filter geostat by area + tests
ymh <ymh.work@gmail.com>
parents: 308
diff changeset
    35
                    "geonames_hierarchy" => $area
0fce13da58af filter geostat by area + tests
ymh <ymh.work@gmail.com>
parents: 308
diff changeset
    36
                ]
0fce13da58af filter geostat by area + tests
ymh <ymh.work@gmail.com>
parents: 308
diff changeset
    37
            ];
0fce13da58af filter geostat by area + tests
ymh <ymh.work@gmail.com>
parents: 308
diff changeset
    38
        }
378
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    39
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    40
        $query = $filterManager->buildQuery($qFilterParts);
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    41
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    42
        $queryES = [
406
cf0f23803a53 upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents: 378
diff changeset
    43
            'index' => config('elasticsearch.index'),
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
            'body' => [
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
                "size" => 0,
378
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    46
                "query" => $query,
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
                "aggs" => [
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
                    "geos" => [
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
                        "terms" => [
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
                            "size" => 0,
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
                            "field" => "geonames_hierarchy"
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
                        ]
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
                    ]
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
                ]
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
            ]
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
        ];
378
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    57
        $esRes = Es::search($queryES);
308
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        $geosats = [];
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        foreach($esRes['aggregations']['geos']['buckets'] as $bucket) {
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
            $geosats[(string)($bucket['key'])] = $bucket['doc_count'];
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
        }
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
        return response()->json(['geostats' => $geosats ]);
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
    }
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
}