delete elasticsearch index when indexing to clean up properly
[phorkie.git] / src / phorkie / Database / Adapter / Elasticsearch / Setup.php
1 <?php
2 namespace phorkie;
3
4 class Database_Adapter_Elasticsearch_Setup implements Database_ISetup
5 {
6     public function __construct()
7     {
8         $this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
9     }
10
11     public function setup()
12     {
13         $r = new \HTTP_Request2(
14             $this->searchInstance . '/_mapping', \HTTP_Request2::METHOD_GET
15         );
16         $res = $r->send();
17         if ($res->getStatus() == 404) {
18             $this->reset();
19         }
20     }
21
22     public function reset()
23     {
24         $r = new \HTTP_Request2(
25             $this->searchInstance,
26             \HTTP_Request2::METHOD_DELETE
27         );
28         $r->send();
29
30         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
31             $this->searchInstance,
32             \HTTP_Request2::METHOD_PUT
33         );
34         $r->send();
35
36         //create mapping
37         //mapping for repositories
38         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
39             $this->searchInstance . 'repo/_mapping',
40             \HTTP_Request2::METHOD_PUT
41         );
42         $r->setBody(
43             json_encode(
44                 (object)array(
45                     'repo' => (object)array(
46                         '_timestamp' => (object)array(
47                             'enabled' => true,
48                             'path'    => 'tstamp',
49                         ),
50                         'properties' => (object)array(
51                             'id' => (object)array(
52                                 'type' => 'long'
53                             ),
54                             'description' => (object)array(
55                                 'type'  => 'string',
56                                 'boost' => 2.0
57                             ),
58                             'crdate' => (object)array(
59                                 //creation date
60                                 'type' => 'date',
61                             ),
62                             'tstamp' => (object)array(
63                                 //modification date
64                                 'type' => 'date',
65                             )
66                         )
67                     )
68                 )
69             )
70         );
71         $r->send();
72
73         //mapping for files
74         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
75             $this->searchInstance . 'file/_mapping',
76             \HTTP_Request2::METHOD_PUT
77         );
78         $r->setBody(
79             json_encode(
80                 (object)array(
81                     'file' => (object)array(
82                         '_parent' => (object)array(
83                             'type' => 'repo'
84                         ),
85                         'properties' => (object)array(
86                             'name' => (object)array(
87                                 'type'  => 'string',
88                                 'boost' => 1.5
89                             ),
90                             'extension' => (object)array(
91                                 'type'  => 'string',
92                                 'boost' => 1.0
93                             ),
94                             'content' => (object)array(
95                                 'type'  => 'string',
96                                 'boost' => 0.8
97                             )
98                         )
99                     )
100                 )
101             )
102         );
103         $r->send();
104     }
105
106 }
107
108 ?>