diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/phorkie/Database.php | 4 | ||||
| -rw-r--r-- | src/phorkie/Database/Adapter/Elasticsearch/HTTPRequest.php | 25 | ||||
| -rw-r--r-- | src/phorkie/Database/Setup/Elasticsearch.php | 22 | ||||
| -rw-r--r-- | src/phorkie/Indexer/Elasticsearch.php | 60 | ||||
| -rw-r--r-- | src/phorkie/Search/Elasticsearch.php | 2 |
5 files changed, 96 insertions, 17 deletions
diff --git a/src/phorkie/Database.php b/src/phorkie/Database.php index 9df6d8c..e9a75a1 100644 --- a/src/phorkie/Database.php +++ b/src/phorkie/Database.php @@ -13,6 +13,10 @@ class Database return new Indexer_Elasticsearch(); } + public function getSetup() + { + return new Database_Setup_Elasticsearch(); + } } ?> diff --git a/src/phorkie/Database/Adapter/Elasticsearch/HTTPRequest.php b/src/phorkie/Database/Adapter/Elasticsearch/HTTPRequest.php new file mode 100644 index 0000000..2d74a4e --- /dev/null +++ b/src/phorkie/Database/Adapter/Elasticsearch/HTTPRequest.php @@ -0,0 +1,25 @@ +<?php +namespace phorkie; + +class Database_Adapter_Elasticsearch_HTTPRequest extends \HTTP_Request2 +{ + public function send() + { + $res = parent::send(); + $mainCode = intval($res->getStatus() / 100); + if ($mainCode != 2) { + $js = json_decode($res->getBody()); + if (isset($js->error)) { + $error = $js->error; + } else { + $error = $res->getBody(); + } + throw new Exception( + 'Error in elasticsearch communication: ' . $error + ); + } + return $res; + } +} + +?> diff --git a/src/phorkie/Database/Setup/Elasticsearch.php b/src/phorkie/Database/Setup/Elasticsearch.php index acf4bfc..b5e2005 100644 --- a/src/phorkie/Database/Setup/Elasticsearch.php +++ b/src/phorkie/Database/Setup/Elasticsearch.php @@ -10,9 +10,15 @@ class Database_Setup_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 \HTTP_Request2( + $r = new Database_Adapter_Elasticsearch_HTTPRequest( $this->searchInstance . 'repo/_mapping', \HTTP_Request2::METHOD_PUT ); @@ -20,6 +26,10 @@ class Database_Setup_Elasticsearch json_encode( (object)array( 'repo' => (object)array( + '_timestamp' => (object)array( + 'enabled' => true, + 'path' => 'tstamp', + ), 'properties' => (object)array( 'id' => (object)array( 'type' => 'long' @@ -27,6 +37,14 @@ class Database_Setup_Elasticsearch 'description' => (object)array( 'type' => 'string', 'boost' => 2.0 + ), + 'crdate' => (object)array( + //creation date + 'type' => 'date', + ), + 'tstamp' => (object)array( + //modification date + 'type' => 'date', ) ) ) @@ -36,7 +54,7 @@ class Database_Setup_Elasticsearch $r->send(); //mapping for files - $r = new \HTTP_Request2( + $r = new Database_Adapter_Elasticsearch_HTTPRequest( $this->searchInstance . 'file/_mapping', \HTTP_Request2::METHOD_PUT ); diff --git a/src/phorkie/Indexer/Elasticsearch.php b/src/phorkie/Indexer/Elasticsearch.php index 2921239..077865e 100644 --- a/src/phorkie/Indexer/Elasticsearch.php +++ b/src/phorkie/Indexer/Elasticsearch.php @@ -9,26 +9,30 @@ class Indexer_Elasticsearch } - public function addRepo(Repository $repo) + public function addRepo(Repository $repo, $crdate = null) { - $this->updateRepo($repo); + if ($crdate == null) { + $crdate = time(); + } + $this->updateRepo($repo, $crdate); } - public function updateRepo(Repository $repo) + public function updateRepo(Repository $repo, $crdate = null) { //add repository - $r = new \HTTP_Request2( + $r = new Database_Adapter_Elasticsearch_HTTPRequest( $this->searchInstance . 'repo/' . $repo->id, \HTTP_Request2::METHOD_PUT ); - $r->setBody( - json_encode( - (object)array( - 'id' => $repo->id, - 'description' => $repo->getDescription(), - ) - ) + $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 @@ -36,7 +40,7 @@ class Indexer_Elasticsearch $this->deleteRepoFiles($repo); foreach ($repo->getFiles() as $file) { - $r = new \HTTP_Request2( + $r = new Database_Adapter_Elasticsearch_HTTPRequest( $this->searchInstance . 'file/?parent=' . $repo->id, \HTTP_Request2::METHOD_POST ); @@ -53,10 +57,38 @@ class Indexer_Elasticsearch } } + 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 \HTTP_Request2( + $r = new Database_Adapter_Elasticsearch_HTTPRequest( $this->searchInstance . 'repo/' . $repo->id, \HTTP_Request2::METHOD_DELETE ); @@ -68,7 +100,7 @@ class Indexer_Elasticsearch protected function deleteRepoFiles(Repository $repo) { //delete files of that repository - $r = new \HTTP_Request2( + $r = new Database_Adapter_Elasticsearch_HTTPRequest( $this->searchInstance . 'file/_query', \HTTP_Request2::METHOD_DELETE ); diff --git a/src/phorkie/Search/Elasticsearch.php b/src/phorkie/Search/Elasticsearch.php index 0ed766b..06a0cfe 100644 --- a/src/phorkie/Search/Elasticsearch.php +++ b/src/phorkie/Search/Elasticsearch.php @@ -20,7 +20,7 @@ class Search_Elasticsearch */ public function search($term, $page = 0, $perPage = 10) { - $r = new \HTTP_Request2( + $r = new Database_Adapter_Elasticsearch_HTTPRequest( $this->searchInstance . 'repo/_search', \HTTP_Request2::METHOD_GET ); |
