server/src/app/Http/Controllers/Api/DiscourseController.php
author ymh <ymh.work@gmail.com>
Tue, 22 Nov 2016 13:53:52 +0100
changeset 430 46b4d1971fee
parent 407 2dba812c7ef2
child 465 9df336529b2f
permissions -rw-r--r--
Correct filter Manager refactorisation into an object
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
160
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
namespace CorpusParole\Http\Controllers\Api;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
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
     6
use CorpusParole\Libraries\Filters\CorpusFilterManager;
160
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
use Illuminate\Http\Request;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
377
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    10
use Es;
160
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
class DiscourseController extends Controller
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
{
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    /**
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
     * Display the specified resource.
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
     *
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
     * @return \Illuminate\Http\Response
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
     */
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    public function index(Request $request)
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    {
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
378
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    23
        $filterManager = new CorpusFilterManager();
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    24
        $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
    25
        unset($filters['discourses']);
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    26
        $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
    27
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    28
        $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
    29
5b47eab083f3 implement filters on stats controller, put all filter logic in CorpusFilterManager
ymh <ymh.work@gmail.com>
parents: 377
diff changeset
    30
160
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
377
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    32
        $esQuery = [
406
cf0f23803a53 upgrade elasticsearch to 5.0, upgrade ember
ymh <ymh.work@gmail.com>
parents: 378
diff changeset
    33
            'index' => config('elasticsearch.index'),
377
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    34
            'body' => [
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    35
                "size" => 0,
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    36
                "query" => $query,
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    37
                "aggs" => [
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    38
                    "discourses" => [
407
2dba812c7ef2 add a way to build rpm for puppet files, correct elasticsearch provisioning, correct error on elasticsearch queries + tests
ymh <ymh.work@gmail.com>
parents: 406
diff changeset
    39
                        "terms" => [ "field" => "discourse_types", "order" => [ "_count" => "desc" ], "size" => 2147483647 ]
377
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    40
                    ]
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    41
                ]
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    42
            ]
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    43
        ];
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    44
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    45
        $esRes = Es::search($esQuery);
160
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        $discourses = [];
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
377
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    49
        foreach ($esRes['aggregations']['discourses']['buckets'] as $b) {
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    50
            $key = $b['key'];
52169c718513 make all stats view use elasticsearch. Make sure that the document discourse types are taken form the predefined list
ymh <ymh.work@gmail.com>
parents: 307
diff changeset
    51
            $count = $b['doc_count'];
160
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
            $label = config('corpusparole.corpus_discourse_type')[$key];
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
            $discourses[$key] = [
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
                "label" => $label,
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
                "count" => $count
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            ];
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        }
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        return response()->json(['discourses' => $discourses ]);
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
    }
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
}