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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
<?php
namespace phorkie;
class Database_Adapter_Elasticsearch_Search implements Database_ISearch
{
protected static $sortMap = array(
'id' => array('id', 'asc'),
'crdate' => array('crdate', 'desc'),
'tstamp' => array('tstamp', 'desc'),
);
public function __construct()
{
$this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
}
/**
* List all repositories
*
* @param integer $page Page of search results, starting with 0
* @param integer $perPage Number of results per page
* @param string $sort Sort order. Allowed values:
* - id - repository id
* - crdate - creation date
* - tstamp - modification date
*
* @return Search_Result Search result object
*/
public function listAll($page = 0, $perPage = 10, $sort = 'id', $sortOrder = null)
{
list($sortField, $orderField) = $this->getSortField($sort, $sortOrder);
$r = new Database_Adapter_Elasticsearch_HTTPRequest(
$this->searchInstance . 'repo/_search',
\HTTP_Request2::METHOD_GET
);
$r->setBody(
json_encode(
(object)array(
'from' => $page * $perPage,
'size' => $perPage,
'sort' => array(
$sortField => $orderField
),
'query' => (object)array(
'match_all' => (object)array()
),
)
)
);
$httpRes = $r->send();
$jRes = json_decode($httpRes->getBody());
if (isset($jRes->error)) {
throw new Exception(
'Search exception: ' . $jRes->error, $jRes->status
);
}
$sres = new Search_Result();
$sres->results = $jRes->hits->total;
$sres->page = $page;
$sres->perPage = $perPage;
foreach ($jRes->hits->hits as $hit) {
$r = new Repository();
$r->loadById($hit->_source->id);
$r->crdate = strtotime($hit->_source->crdate);
$sres->repos[] = $r;
}
return $sres;
}
/**
* Search for a given term and return repositories that contain it
* in their description, file names or file content
*
* @param string $term Search term
* @param integer $page Page of search results, starting with 0
* @param integer $perPage Number of results per page
*
* @return Search_Result Search result object
*/
public function search($term, $page = 0, $perPage = 10)
{
$r = new Database_Adapter_Elasticsearch_HTTPRequest(
$this->searchInstance . 'repo/_search',
\HTTP_Request2::METHOD_GET
);
$r->setBody(
json_encode(
(object)array(
'from' => $page * $perPage,
'size' => $perPage,
'query' => (object)array(
'bool' => (object)array(
'should' => array(
(object)array(
'query_string' => (object)array(
'query' => $term,
'default_operator' => 'AND'
),
),
(object)array(
'has_child' => (object)array(
'type' => 'file',
'query' => (object)array(
'query_string' => (object)array(
'query' => $term,
'default_operator' => 'AND'
)
)
)
)
)
)
)
)
)
);
$httpRes = $r->send();
$jRes = json_decode($httpRes->getBody());
if (isset($jRes->error)) {
throw new Exception(
'Search exception: ' . $jRes->error, $jRes->status
);
}
$sres = new Search_Result();
$sres->results = $jRes->hits->total;
$sres->page = $page;
$sres->perPage = $perPage;
foreach ($jRes->hits->hits as $hit) {
$r = new Repository();
//FIXME: error handling. what about deleted repos?
$r->loadById($hit->_source->id);
$sres->repos[] = $r;
}
return $sres;
}
protected function getSortField($sort, $sortOrder)
{
if (!isset(self::$sortMap[$sort])) {
throw new Exception('Invalid sort parameter: ' . $sort);
}
if ($sortOrder !== 'asc' && $sortOrder !== 'desc') {
throw new Exception('Invalid sortOrder parameter: ' . $sortOrder);
}
$data = self::$sortMap[$sort];
if ($sortOrder !== null) {
$data[1] = $sortOrder;
}
return $data;
}
}
?>
|