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 --- src/phorkie/Database.php | 6 +- .../Database/Adapter/Elasticsearch/Indexer.php | 121 +++++++++++++++++++++ .../Database/Adapter/Elasticsearch/Search.php | 82 ++++++++++++++ .../Database/Adapter/Elasticsearch/Setup.php | 91 ++++++++++++++++ src/phorkie/Database/Setup/Elasticsearch.php | 91 ---------------- src/phorkie/Indexer/Elasticsearch.php | 121 --------------------- src/phorkie/Search/Elasticsearch.php | 82 -------------- 7 files changed, 297 insertions(+), 297 deletions(-) create mode 100644 src/phorkie/Database/Adapter/Elasticsearch/Indexer.php create mode 100644 src/phorkie/Database/Adapter/Elasticsearch/Search.php create mode 100644 src/phorkie/Database/Adapter/Elasticsearch/Setup.php delete mode 100644 src/phorkie/Database/Setup/Elasticsearch.php delete mode 100644 src/phorkie/Indexer/Elasticsearch.php delete mode 100644 src/phorkie/Search/Elasticsearch.php (limited to 'src') diff --git a/src/phorkie/Database.php b/src/phorkie/Database.php index e9a75a1..9854e7c 100644 --- a/src/phorkie/Database.php +++ b/src/phorkie/Database.php @@ -5,17 +5,17 @@ class Database { public function getSearch() { - return new Search_Elasticsearch(); + return new Database_Adapter_Elasticsearch_Search(); } public function getIndexer() { - return new Indexer_Elasticsearch(); + return new Database_Adapter_Elasticsearch_Indexer(); } public function getSetup() { - return new Database_Setup_Elasticsearch(); + return new Database_Adapter_Elasticsearch_Setup(); } } diff --git a/src/phorkie/Database/Adapter/Elasticsearch/Indexer.php b/src/phorkie/Database/Adapter/Elasticsearch/Indexer.php new file mode 100644 index 0000000..d9d1032 --- /dev/null +++ b/src/phorkie/Database/Adapter/Elasticsearch/Indexer.php @@ -0,0 +1,121 @@ +searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch']; + } + + + public function addRepo(Repository $repo, $crdate = null) + { + if ($crdate == null) { + $crdate = time(); + } + $this->updateRepo($repo, $crdate); + } + + public function updateRepo(Repository $repo, $crdate = null) + { + //add repository + $r = new Database_Adapter_Elasticsearch_HTTPRequest( + $this->searchInstance . 'repo/' . $repo->id, + \HTTP_Request2::METHOD_PUT + ); + $repoData = array( + 'id' => $repo->id, + 'description' => $repo->getDescription(), + 'tstamp' => gmdate('c', time()), + ); + if ($crdate !== null) { + $repoData['crdate'] = gmdate('c', $crdate); + } + $r->setBody(json_encode((object)$repoData)); + $r->send(); + + //add files + //clean up before adding files; files might have been deleted + $this->deleteRepoFiles($repo); + + foreach ($repo->getFiles() as $file) { + $r = new Database_Adapter_Elasticsearch_HTTPRequest( + $this->searchInstance . 'file/?parent=' . $repo->id, + \HTTP_Request2::METHOD_POST + ); + $r->setBody( + json_encode( + (object)array( + 'name' => $file->getFilename(), + 'extension' => $file->getExt(), + 'content' => $file->isText() ? $file->getContent() : '', + ) + ) + ); + $r->send(); + } + } + + public function deleteAllRepos() + { + $r = new Database_Adapter_Elasticsearch_HTTPRequest( + $this->searchInstance . 'repo/_query', + \HTTP_Request2::METHOD_DELETE + ); + $r->setBody( + json_encode( + (object)array( + 'match_all' => (object)array() + ) + ) + ); + $r->send(); + $r = new Database_Adapter_Elasticsearch_HTTPRequest( + $this->searchInstance . 'file/_query', + \HTTP_Request2::METHOD_DELETE + ); + $r->setBody( + json_encode( + (object)array( + 'match_all' => (object)array() + ) + ) + ); + $r->send(); + } + + public function deleteRepo(Repository $repo) + { + //delete repository from index + $r = new Database_Adapter_Elasticsearch_HTTPRequest( + $this->searchInstance . 'repo/' . $repo->id, + \HTTP_Request2::METHOD_DELETE + ); + $r->send(); + + $this->deleteRepoFiles($repo); + } + + protected function deleteRepoFiles(Repository $repo) + { + //delete files of that repository + $r = new Database_Adapter_Elasticsearch_HTTPRequest( + $this->searchInstance . 'file/_query', + \HTTP_Request2::METHOD_DELETE + ); + $r->setBody( + json_encode( + (object)array( + 'field' => (object)array( + '_parent' => $repo->id + ) + ) + ) + ); + $r->send(); + } + +} + +?> 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; + } +} + +?> diff --git a/src/phorkie/Database/Adapter/Elasticsearch/Setup.php b/src/phorkie/Database/Adapter/Elasticsearch/Setup.php new file mode 100644 index 0000000..acbe77c --- /dev/null +++ b/src/phorkie/Database/Adapter/Elasticsearch/Setup.php @@ -0,0 +1,91 @@ +searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch']; + } + + public function setup() + { + $r = new Database_Adapter_Elasticsearch_HTTPRequest( + $this->searchInstance . 'repo/_mapping', + \HTTP_Request2::METHOD_DELETE + ); + $r->send(); + + //create mapping + //mapping for repositories + $r = new Database_Adapter_Elasticsearch_HTTPRequest( + $this->searchInstance . 'repo/_mapping', + \HTTP_Request2::METHOD_PUT + ); + $r->setBody( + json_encode( + (object)array( + 'repo' => (object)array( + '_timestamp' => (object)array( + 'enabled' => true, + 'path' => 'tstamp', + ), + 'properties' => (object)array( + 'id' => (object)array( + 'type' => 'long' + ), + 'description' => (object)array( + 'type' => 'string', + 'boost' => 2.0 + ), + 'crdate' => (object)array( + //creation date + 'type' => 'date', + ), + 'tstamp' => (object)array( + //modification date + 'type' => 'date', + ) + ) + ) + ) + ) + ); + $r->send(); + + //mapping for files + $r = new Database_Adapter_Elasticsearch_HTTPRequest( + $this->searchInstance . 'file/_mapping', + \HTTP_Request2::METHOD_PUT + ); + $r->setBody( + json_encode( + (object)array( + 'file' => (object)array( + '_parent' => (object)array( + 'type' => 'repo' + ), + 'properties' => (object)array( + 'name' => (object)array( + 'type' => 'string', + 'boost' => 1.5 + ), + 'extension' => (object)array( + 'type' => 'string', + 'boost' => 1.0 + ), + 'content' => (object)array( + 'type' => 'string', + 'boost' => 0.8 + ) + ) + ) + ) + ) + ); + $r->send(); + } + +} + +?> diff --git a/src/phorkie/Database/Setup/Elasticsearch.php b/src/phorkie/Database/Setup/Elasticsearch.php deleted file mode 100644 index b5e2005..0000000 --- a/src/phorkie/Database/Setup/Elasticsearch.php +++ /dev/null @@ -1,91 +0,0 @@ -searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch']; - } - - public function setup() - { - $r = new Database_Adapter_Elasticsearch_HTTPRequest( - $this->searchInstance . 'repo/_mapping', - \HTTP_Request2::METHOD_DELETE - ); - $r->send(); - - //create mapping - //mapping for repositories - $r = new Database_Adapter_Elasticsearch_HTTPRequest( - $this->searchInstance . 'repo/_mapping', - \HTTP_Request2::METHOD_PUT - ); - $r->setBody( - json_encode( - (object)array( - 'repo' => (object)array( - '_timestamp' => (object)array( - 'enabled' => true, - 'path' => 'tstamp', - ), - 'properties' => (object)array( - 'id' => (object)array( - 'type' => 'long' - ), - 'description' => (object)array( - 'type' => 'string', - 'boost' => 2.0 - ), - 'crdate' => (object)array( - //creation date - 'type' => 'date', - ), - 'tstamp' => (object)array( - //modification date - 'type' => 'date', - ) - ) - ) - ) - ) - ); - $r->send(); - - //mapping for files - $r = new Database_Adapter_Elasticsearch_HTTPRequest( - $this->searchInstance . 'file/_mapping', - \HTTP_Request2::METHOD_PUT - ); - $r->setBody( - json_encode( - (object)array( - 'file' => (object)array( - '_parent' => (object)array( - 'type' => 'repo' - ), - 'properties' => (object)array( - 'name' => (object)array( - 'type' => 'string', - 'boost' => 1.5 - ), - 'extension' => (object)array( - 'type' => 'string', - 'boost' => 1.0 - ), - 'content' => (object)array( - 'type' => 'string', - 'boost' => 0.8 - ) - ) - ) - ) - ) - ); - $r->send(); - } - -} - -?> diff --git a/src/phorkie/Indexer/Elasticsearch.php b/src/phorkie/Indexer/Elasticsearch.php deleted file mode 100644 index 077865e..0000000 --- a/src/phorkie/Indexer/Elasticsearch.php +++ /dev/null @@ -1,121 +0,0 @@ -searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch']; - } - - - public function addRepo(Repository $repo, $crdate = null) - { - if ($crdate == null) { - $crdate = time(); - } - $this->updateRepo($repo, $crdate); - } - - public function updateRepo(Repository $repo, $crdate = null) - { - //add repository - $r = new Database_Adapter_Elasticsearch_HTTPRequest( - $this->searchInstance . 'repo/' . $repo->id, - \HTTP_Request2::METHOD_PUT - ); - $repoData = array( - 'id' => $repo->id, - 'description' => $repo->getDescription(), - 'tstamp' => gmdate('c', time()), - ); - if ($crdate !== null) { - $repoData['crdate'] = gmdate('c', $crdate); - } - $r->setBody(json_encode((object)$repoData)); - $r->send(); - - //add files - //clean up before adding files; files might have been deleted - $this->deleteRepoFiles($repo); - - foreach ($repo->getFiles() as $file) { - $r = new Database_Adapter_Elasticsearch_HTTPRequest( - $this->searchInstance . 'file/?parent=' . $repo->id, - \HTTP_Request2::METHOD_POST - ); - $r->setBody( - json_encode( - (object)array( - 'name' => $file->getFilename(), - 'extension' => $file->getExt(), - 'content' => $file->isText() ? $file->getContent() : '', - ) - ) - ); - $r->send(); - } - } - - public function deleteAllRepos() - { - $r = new Database_Adapter_Elasticsearch_HTTPRequest( - $this->searchInstance . 'repo/_query', - \HTTP_Request2::METHOD_DELETE - ); - $r->setBody( - json_encode( - (object)array( - 'match_all' => (object)array() - ) - ) - ); - $r->send(); - $r = new Database_Adapter_Elasticsearch_HTTPRequest( - $this->searchInstance . 'file/_query', - \HTTP_Request2::METHOD_DELETE - ); - $r->setBody( - json_encode( - (object)array( - 'match_all' => (object)array() - ) - ) - ); - $r->send(); - } - - public function deleteRepo(Repository $repo) - { - //delete repository from index - $r = new Database_Adapter_Elasticsearch_HTTPRequest( - $this->searchInstance . 'repo/' . $repo->id, - \HTTP_Request2::METHOD_DELETE - ); - $r->send(); - - $this->deleteRepoFiles($repo); - } - - protected function deleteRepoFiles(Repository $repo) - { - //delete files of that repository - $r = new Database_Adapter_Elasticsearch_HTTPRequest( - $this->searchInstance . 'file/_query', - \HTTP_Request2::METHOD_DELETE - ); - $r->setBody( - json_encode( - (object)array( - 'field' => (object)array( - '_parent' => $repo->id - ) - ) - ) - ); - $r->send(); - } - -} - -?> diff --git a/src/phorkie/Search/Elasticsearch.php b/src/phorkie/Search/Elasticsearch.php deleted file mode 100644 index 06a0cfe..0000000 --- a/src/phorkie/Search/Elasticsearch.php +++ /dev/null @@ -1,82 +0,0 @@ -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