4 class Database_Adapter_Elasticsearch_Search implements Database_ISearch
6 protected static $sortMap = array(
7 'id' => array('id', 'asc'),
8 'crdate' => array('crdate', 'desc'),
9 'modate' => array('modate', 'desc'),
10 'tstamp' => array('tstamp', 'desc'),
13 public function __construct()
15 $this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
19 * List all repositories
21 * @param integer $page Page of search results, starting with 0
22 * @param integer $perPage Number of results per page
23 * @param string $sort Sort order. Allowed values:
24 * - id - repository id
25 * - crdate - creation date
26 * - modate - modification date
27 * - tstamp - last index date
29 * @return Search_Result Search result object
31 public function listAll($page = 0, $perPage = 10, $sort = 'id', $sortOrder = null)
33 list($sortField, $orderField) = $this->getSortField($sort, $sortOrder);
34 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
35 $this->searchInstance . 'repo/_search',
36 \HTTP_Request2::METHOD_GET
41 'from' => $page * $perPage,
44 $sortField => $orderField
46 'query' => (object)array(
47 'match_all' => (object)array()
52 $httpRes = $r->send();
53 $jRes = json_decode($httpRes->getBody());
54 if (isset($jRes->error)) {
56 'Search exception: ' . $jRes->error, $jRes->status
60 $sres = new Search_Result();
61 $sres->results = $jRes->hits->total;
63 $sres->perPage = $perPage;
65 foreach ($jRes->hits->hits as $hit) {
66 $r = new Repository();
68 $r->loadById($hit->_source->id);
69 } catch (Exception_NotFound $e) {
72 $r->crdate = strtotime($hit->_source->crdate);
73 $r->modate = strtotime($hit->_source->modate);
82 * Search for a given term and return repositories that contain it
83 * in their description, file names or file content
85 * @param string $term Search term
86 * @param integer $page Page of search results, starting with 0
87 * @param integer $perPage Number of results per page
89 * @return Search_Result Search result object
91 public function search($term, $page = 0, $perPage = 10)
93 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
94 $this->searchInstance . 'repo/_search',
95 \HTTP_Request2::METHOD_GET
100 'from' => $page * $perPage,
102 'query' => (object)array(
103 'bool' => (object)array(
106 'query_string' => (object)array(
108 'default_operator' => 'AND'
112 'has_child' => (object)array(
114 'query' => (object)array(
115 'query_string' => (object)array(
117 'default_operator' => 'AND'
128 $httpRes = $r->send();
129 $jRes = json_decode($httpRes->getBody());
130 if (isset($jRes->error)) {
132 'Search exception: ' . $jRes->error, $jRes->status
136 $sres = new Search_Result();
137 $sres->results = $jRes->hits->total;
139 $sres->perPage = $perPage;
141 foreach ($jRes->hits->hits as $hit) {
142 $r = new Repository();
143 //FIXME: error handling. what about deleted repos?
144 $r->loadById($hit->_source->id);
151 protected function getSortField($sort, $sortOrder)
153 if (!isset(self::$sortMap[$sort])) {
154 throw new Exception('Invalid sort parameter: ' . $sort);
156 if ($sortOrder !== 'asc' && $sortOrder !== 'desc') {
157 throw new Exception('Invalid sortOrder parameter: ' . $sortOrder);
160 $data = self::$sortMap[$sort];
161 if ($sortOrder !== null) {
162 $data[1] = $sortOrder;