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 'tstamp' => array('tstamp', 'desc'),
12 public function __construct()
14 $this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
18 * List all repositories
20 * @param integer $page Page of search results, starting with 0
21 * @param integer $perPage Number of results per page
22 * @param string $sort Sort order. Allowed values:
23 * - id - repository id
24 * - crdate - creation date
25 * - tstamp - modification date
27 * @return Search_Result Search result object
29 public function listAll($page = 0, $perPage = 10, $sort = 'id', $sortOrder = null)
31 list($sortField, $orderField) = $this->getSortField($sort, $sortOrder);
32 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
33 $this->searchInstance . 'repo/_search',
34 \HTTP_Request2::METHOD_GET
39 'from' => $page * $perPage,
42 $sortField => $orderField
44 'query' => (object)array(
45 'match_all' => (object)array()
50 $httpRes = $r->send();
51 $jRes = json_decode($httpRes->getBody());
52 if (isset($jRes->error)) {
54 'Search exception: ' . $jRes->error, $jRes->status
58 $sres = new Search_Result();
59 $sres->results = $jRes->hits->total;
61 $sres->perPage = $perPage;
63 foreach ($jRes->hits->hits as $hit) {
64 $r = new Repository();
65 $r->loadById($hit->_source->id);
66 $r->crdate = strtotime($hit->_source->crdate);
75 * Search for a given term and return repositories that contain it
76 * in their description, file names or file content
78 * @param string $term Search term
79 * @param integer $page Page of search results, starting with 0
80 * @param integer $perPage Number of results per page
82 * @return Search_Result Search result object
84 public function search($term, $page = 0, $perPage = 10)
86 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
87 $this->searchInstance . 'repo/_search',
88 \HTTP_Request2::METHOD_GET
93 'from' => $page * $perPage,
95 'query' => (object)array(
96 'bool' => (object)array(
99 'query_string' => (object)array(
101 'default_operator' => 'AND'
105 'has_child' => (object)array(
107 'query' => (object)array(
108 'query_string' => (object)array(
110 'default_operator' => 'AND'
121 $httpRes = $r->send();
122 $jRes = json_decode($httpRes->getBody());
123 if (isset($jRes->error)) {
125 'Search exception: ' . $jRes->error, $jRes->status
129 $sres = new Search_Result();
130 $sres->results = $jRes->hits->total;
132 $sres->perPage = $perPage;
134 foreach ($jRes->hits->hits as $hit) {
135 $r = new Repository();
136 //FIXME: error handling. what about deleted repos?
137 $r->loadById($hit->_source->id);
144 protected function getSortField($sort, $sortOrder)
146 if (!isset(self::$sortMap[$sort])) {
147 throw new Exception('Invalid sort parameter: ' . $sort);
149 if ($sortOrder !== 'asc' && $sortOrder !== 'desc') {
150 throw new Exception('Invalid sortOrder parameter: ' . $sortOrder);
153 $data = self::$sortMap[$sort];
154 if ($sortOrder !== null) {
155 $data[1] = $sortOrder;