server/src/app/Http/Controllers/Api/GeoStatsController.php
author ymh <ymh.work@gmail.com>
Thu, 20 Oct 2016 17:27:36 +0200
changeset 377 52169c718513
parent 320 0fce13da58af
child 378 5b47eab083f3
permissions -rw-r--r--
make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list

<?php

namespace CorpusParole\Http\Controllers\Api;

use Illuminate\Http\Request;

use CorpusParole\Http\Controllers\Controller;
use Es;
use Log;

class GeoStatsController extends Controller
{
    /**
     * Display the specified resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $area = $request->input('area');
        $filter = [
            'match_all' => []
        ];
        if(!is_null($area) && $area !== config('corpusparole.geonames_earth_geonamesid')) {
            $filter = [
                'term' => [
                    "geonames_hierarchy" => $area
                ]
            ];
        }
        $query = [
            'index' => env('ELASTICSEARCH_INDEX'),
            'body' => [
                "size" => 0,
                "query" => $filter,
                "aggs" => [
                    "geos" => [
                        "terms" => [
                            "size" => 0,
                            "field" => "geonames_hierarchy"
                        ]
                    ]
                ]
            ]
        ];
        $esRes = Es::search($query);

        $geosats = [];

        foreach($esRes['aggregations']['geos']['buckets'] as $bucket) {
            $geosats[(string)($bucket['key'])] = $bucket['doc_count'];
        }

        return response()->json(['geostats' => $geosats ]);
    }
}