aboutsummaryrefslogtreecommitdiff
path: root/src/phorkie/Database/Adapter/Elasticsearch/Search.php
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2012-05-07 20:47:01 +0200
committerChristian Weiske <cweiske@cweiske.de>2012-05-07 20:47:01 +0200
commit7c5ea8dded583601f29c7ea9bcb507ecbdd854d6 (patch)
tree53426dba320005d5f058250e20e239d454080d76 /src/phorkie/Database/Adapter/Elasticsearch/Search.php
parent3c83676016bce1727f0046f4aad7865be8a71fd4 (diff)
downloadphorkie-7c5ea8dded583601f29c7ea9bcb507ecbdd854d6.tar.gz
phorkie-7c5ea8dded583601f29c7ea9bcb507ecbdd854d6.zip
move elasticsearch code in one folder
Diffstat (limited to 'src/phorkie/Database/Adapter/Elasticsearch/Search.php')
-rw-r--r--src/phorkie/Database/Adapter/Elasticsearch/Search.php82
1 files changed, 82 insertions, 0 deletions
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 @@
+<?php
+namespace phorkie;
+
+class Database_Adapter_Elasticsearch_Search
+{
+ public function __construct()
+ {
+ $this->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;
+ }
+}
+
+?>