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();
67 $r->loadById($hit->_source->id);
68 $r->crdate = strtotime($hit->_source->crdate);
69 $r->modate = strtotime($hit->_source->modate);
78 * Search for a given term and return repositories that contain it
79 * in their description, file names or file content
81 * @param string $term Search term
82 * @param integer $page Page of search results, starting with 0
83 * @param integer $perPage Number of results per page
85 * @return Search_Result Search result object
87 public function search($term, $page = 0, $perPage = 10)
89 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
90 $this->searchInstance . 'repo/_search',
91 \HTTP_Request2::METHOD_GET
96 'from' => $page * $perPage,
98 'query' => (object)array(
99 'bool' => (object)array(
102 'query_string' => (object)array(
104 'default_operator' => 'AND'
108 'has_child' => (object)array(
110 'query' => (object)array(
111 'query_string' => (object)array(
113 'default_operator' => 'AND'
124 $httpRes = $r->send();
125 $jRes = json_decode($httpRes->getBody());
126 if (isset($jRes->error)) {
128 'Search exception: ' . $jRes->error, $jRes->status
132 $sres = new Search_Result();
133 $sres->results = $jRes->hits->total;
135 $sres->perPage = $perPage;
137 foreach ($jRes->hits->hits as $hit) {
138 $r = new Repository();
139 //FIXME: error handling. what about deleted repos?
140 $r->loadById($hit->_source->id);
147 protected function getSortField($sort, $sortOrder)
149 if (!isset(self::$sortMap[$sort])) {
150 throw new Exception('Invalid sort parameter: ' . $sort);
152 if ($sortOrder !== 'asc' && $sortOrder !== 'desc') {
153 throw new Exception('Invalid sortOrder parameter: ' . $sortOrder);
156 $data = self::$sortMap[$sort];
157 if ($sortOrder !== null) {
158 $data[1] = $sortOrder;