server/src/app/Http/Controllers/Api/GeoStatsController.php
author ymh <ymh.work@gmail.com>
Sun, 02 Oct 2016 11:49:00 +0200
changeset 308 e032d686d88e
child 320 0fce13da58af
permissions -rw-r--r--
add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
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\Requests;
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
use CorpusParole\Http\Controllers\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
     9
use Es;
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
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
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
    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
    /**
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
     * 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
    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
     * @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
    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
    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
    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
        $query = [
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
            'index' => env('ELASTICSEARCH_INDEX'),
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
            '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
    23
                "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
    24
                "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
    25
                    "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
    26
                        "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
    27
                            "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
    28
                            "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
    29
                        ]
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
                    ]
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
                ]
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
            ]
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
        ];
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        $esRes = Es::search($query);
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        $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
    37
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        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
    39
            $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
    40
        }
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
e032d686d88e add hierarchy info in document indexation + geostats api controllers + add some keys to geonames resolver
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        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
    43
    }
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
}