summaryrefslogtreecommitdiff
path: root/src/phorkie/Database/Adapter/Elasticsearch/Setup.php
blob: 0df287475051740bdf8b9bf84de692dbb1080f4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace phorkie;

class Database_Adapter_Elasticsearch_Setup implements Database_ISetup
{
    public function __construct()
    {
        $this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
    }

    public function setup()
    {
        $r = new \HTTP_Request2(
            $this->searchInstance . '/_mapping', \HTTP_Request2::METHOD_GET
        );
        $res = $r->send();
        if ($res->getStatus() == 404) {
            $this->reset();
        }
    }

    public function reset()
    {
        $r = new \HTTP_Request2(
            $this->searchInstance,
            \HTTP_Request2::METHOD_DELETE
        );
        $r->send();

        $r = new Database_Adapter_Elasticsearch_HTTPRequest(
            $this->searchInstance,
            \HTTP_Request2::METHOD_PUT
        );
        $r->send();

        //create mapping
        //mapping for repositories
        $r = new Database_Adapter_Elasticsearch_HTTPRequest(
            $this->searchInstance . 'repo/_mapping',
            \HTTP_Request2::METHOD_PUT
        );
        $r->setBody(
            json_encode(
                (object)array(
                    'repo' => (object)array(
                        '_timestamp' => (object)array(
                            'enabled' => true,
                            'path'    => 'tstamp',
                        ),
                        'properties' => (object)array(
                            'id' => (object)array(
                                'type' => 'long'
                            ),
                            'description' => (object)array(
                                'type'  => 'string',
                                'boost' => 2.0
                            ),
                            'crdate' => (object)array(
                                //creation date
                                'type' => 'date',
                            ),
                            'tstamp' => (object)array(
                                //modification date
                                'type' => 'date',
                            )
                        )
                    )
                )
            )
        );
        $r->send();

        //mapping for files
        $r = new Database_Adapter_Elasticsearch_HTTPRequest(
            $this->searchInstance . 'file/_mapping',
            \HTTP_Request2::METHOD_PUT
        );
        $r->setBody(
            json_encode(
                (object)array(
                    'file' => (object)array(
                        '_parent' => (object)array(
                            'type' => 'repo'
                        ),
                        'properties' => (object)array(
                            'name' => (object)array(
                                'type'  => 'string',
                                'boost' => 1.5
                            ),
                            'extension' => (object)array(
                                'type'  => 'string',
                                'boost' => 1.0
                            ),
                            'content' => (object)array(
                                'type'  => 'string',
                                'boost' => 0.8
                            )
                        )
                    )
                )
            )
        );
        $r->send();
    }

}

?>