aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2012-05-01 20:40:24 +0200
committerChristian Weiske <cweiske@cweiske.de>2012-05-01 20:40:24 +0200
commit1a8bb56c72ad34cf159e03b97a467e5e35a614c5 (patch)
tree9a2d3f9a369aed9c967cf8001a883a2a60f3a002 /src
parentc78bab0c18fb9db18408f1601280997ee8b0779f (diff)
downloadphorkie-1a8bb56c72ad34cf159e03b97a467e5e35a614c5.tar.gz
phorkie-1a8bb56c72ad34cf159e03b97a467e5e35a614c5.zip
searching works now (pager missing)
Diffstat (limited to 'src')
-rw-r--r--src/phorkie/Database.php18
-rw-r--r--src/phorkie/Search/Elasticsearch.php76
-rw-r--r--src/phorkie/Search/Result.php47
3 files changed, 141 insertions, 0 deletions
diff --git a/src/phorkie/Database.php b/src/phorkie/Database.php
new file mode 100644
index 0000000..add38be
--- /dev/null
+++ b/src/phorkie/Database.php
@@ -0,0 +1,18 @@
+<?php
+namespace phorkie;
+
+class Database
+{
+ public function getSearch()
+ {
+ return new Search_Elasticsearch();
+ }
+
+ public function getIndexer()
+ {
+ //FIXME
+ }
+
+}
+
+?>
diff --git a/src/phorkie/Search/Elasticsearch.php b/src/phorkie/Search/Elasticsearch.php
new file mode 100644
index 0000000..d03ef3e
--- /dev/null
+++ b/src/phorkie/Search/Elasticsearch.php
@@ -0,0 +1,76 @@
+<?php
+namespace phorkie;
+
+class Search_Elasticsearch
+{
+ 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 \HTTP_Request2(
+ $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
+ ),
+ ),
+ (object)array(
+ 'has_child' => (object)array(
+ 'type' => 'file',
+ 'query' => (object)array(
+ 'query_string' => (object)array(
+ 'query' => $term
+ )
+ )
+ )
+ )
+ )
+ )
+ )
+ )
+ )
+ );
+ //FIXME: error handling
+ $httpRes = $r->send();
+ $jRes = json_decode($httpRes->getBody());
+
+ $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;
+ }
+}
+
+?>
diff --git a/src/phorkie/Search/Result.php b/src/phorkie/Search/Result.php
new file mode 100644
index 0000000..e64a3a6
--- /dev/null
+++ b/src/phorkie/Search/Result.php
@@ -0,0 +1,47 @@
+<?php
+namespace phorkie;
+
+class Search_Result
+{
+ public $repos;
+ public $results;
+ public $page;
+ public $perPage;
+
+ public function getRepos()
+ {
+ return $this->repos;
+ }
+
+ /**
+ * Returns the number of results
+ *
+ * @return integer Number of results
+ */
+ public function getResults()
+ {
+ return $this->results;
+ }
+
+ /**
+ * Returns the number of the current page, 0 based
+ *
+ * @return integer Number of current page
+ */
+ public function getPage()
+ {
+ return $this->page;
+ }
+
+ /**
+ * Returns the search results per page
+ *
+ * @return integer Number of results per page
+ */
+ public function getPerPage()
+ {
+ return $this->perPage;
+ }
+}
+
+?>