Use commit timestamps for ElasticSearch and sort on last commit time.
[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                             'modate' => (object)array(
63                                 //modification date
64                                 'type' => 'date',
65                             ),
66                             'tstamp' => (object)array(
67                                 //last indexed date
68                                 'type' => 'date',
69                             )
70                         )
71                     )
72                 )
73             )
74         );
75         $r->send();
76
77         //mapping for files
78         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
79             $this->searchInstance . 'file/_mapping',
80             \HTTP_Request2::METHOD_PUT
81         );
82         $r->setBody(
83             json_encode(
84                 (object)array(
85                     'file' => (object)array(
86                         '_parent' => (object)array(
87                             'type' => 'repo'
88                         ),
89                         'properties' => (object)array(
90                             'name' => (object)array(
91                                 'type'  => 'string',
92                                 'boost' => 1.5
93                             ),
94                             'extension' => (object)array(
95                                 'type'  => 'string',
96                                 'boost' => 1.0
97                             ),
98                             'content' => (object)array(
99                                 'type'  => 'string',
100                                 'boost' => 0.8
101                             )
102                         )
103                     )
104                 )
105             )
106         );
107         $r->send();
108     }
109
110 }
111
112 ?>