4 class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer
6 public function __construct()
8 $this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
12 public function addRepo(Repository $repo, $crdate = null)
14 if ($crdate == null) {
17 $this->updateRepo($repo, $crdate);
20 public function updateRepo(Repository $repo, $crdate = null)
23 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
24 $this->searchInstance . 'repo/' . $repo->id,
25 \HTTP_Request2::METHOD_PUT
29 'description' => $repo->getDescription(),
30 'tstamp' => gmdate('c', time()),
32 if ($crdate == null) {
33 $crdate = $this->getCrDate($repo);
35 if ($crdate !== null) {
36 $repoData['crdate'] = gmdate('c', $crdate);
39 $r->setBody(json_encode((object)$repoData));
43 //clean up before adding files; files might have been deleted
44 $this->deleteRepoFiles($repo);
46 foreach ($repo->getFiles() as $file) {
47 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
48 $this->searchInstance . 'file/?parent=' . $repo->id,
49 \HTTP_Request2::METHOD_POST
54 'name' => $file->getFilename(),
55 'extension' => $file->getExt(),
56 'content' => $file->isText() ? $file->getContent() : '',
65 * When updating the repository, we don't have a creation date.
66 * We need to keep it, but elasticsearch does not have a simple way
67 * to update some fields only (without using a custom script).
69 * @return integer Unix timestamp
71 protected function getCrDate(Repository $repo)
73 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
74 $this->searchInstance . 'repo/' . $repo->id,
75 \HTTP_Request2::METHOD_GET
77 $json = json_decode($r->send()->getBody());
79 if (!isset($json->_source->crdate)) {
83 return strtotime($json->_source->crdate);
86 public function deleteAllRepos()
88 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
89 $this->searchInstance . 'repo/_query',
90 \HTTP_Request2::METHOD_DELETE
95 'match_all' => (object)array()
100 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
101 $this->searchInstance . 'file/_query',
102 \HTTP_Request2::METHOD_DELETE
107 'match_all' => (object)array()
114 public function deleteRepo(Repository $repo)
116 //delete repository from index
117 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
118 $this->searchInstance . 'repo/' . $repo->id,
119 \HTTP_Request2::METHOD_DELETE
123 $this->deleteRepoFiles($repo);
126 protected function deleteRepoFiles(Repository $repo)
128 //delete files of that repository
129 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
130 $this->searchInstance . 'file/_query',
131 \HTTP_Request2::METHOD_DELETE
136 'field' => (object)array(
137 '_parent' => $repo->id