From 7c5ea8dded583601f29c7ea9bcb507ecbdd854d6 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Mon, 7 May 2012 20:47:01 +0200 Subject: move elasticsearch code in one folder --- .../Database/Adapter/Elasticsearch/Search.php | 82 ++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/phorkie/Database/Adapter/Elasticsearch/Search.php (limited to 'src/phorkie/Database/Adapter/Elasticsearch/Search.php') diff --git a/src/phorkie/Database/Adapter/Elasticsearch/Search.php b/src/phorkie/Database/Adapter/Elasticsearch/Search.php new file mode 100644 index 0000000..88ad4ac --- /dev/null +++ b/src/phorkie/Database/Adapter/Elasticsearch/Search.php @@ -0,0 +1,82 @@ +searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch']; + } + + /** + * 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; + } +} + +?> -- cgit v1.2.3