server/src/app/Http/Controllers/Api/DiscourseController.php
author ymh <ymh.work@gmail.com>
Thu, 20 Oct 2016 17:27:36 +0200
changeset 377 52169c718513
parent 307 07b44a378ad8
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
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;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
use Illuminate\Http\Request;
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
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
     9
use Es;
160
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
class DiscourseController extends Controller
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
{
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
     * Display the specified resource.
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
     *
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
     * @return \Illuminate\Http\Response
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
     */
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    public function index(Request $request)
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    {
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
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
    22
        $query = [ "match_all" => []];
160
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
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
    24
        $esQuery = [
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
    25
            'index' => env('ELASTICSEARCH_INDEX'),
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
    26
            '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
    27
                "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
    28
                "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
    29
                "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
    30
                    "discourses" => [
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
    31
                        "terms" => [ "field" => "discourse_types", "order" => [ "_count" => "desc" ], "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
    32
                    ]
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
    33
                ]
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
            ]
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
        ];
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
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
        $esRes = Es::search($esQuery);
160
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        $discourses = [];
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
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
    41
        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
    42
            $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
    43
            $count = $b['doc_count'];
160
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
            $label = config('corpusparole.corpus_discourse_type')[$key];
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
            $discourses[$key] = [
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
                "label" => $label,
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
                "count" => $count
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
            ];
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        }
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        return response()->json(['discourses' => $discourses ]);
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    }
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
c77f06ff3e54 add discourse controler + fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
}