/** * @param $type */ public function createIndexIfNotExists($index) { //$indexConfig = $this->configManager->getIndexConfiguration($index); $index = $this->indexRegistry->getIndex($index); if (!$index->exists()) { $index->create(); } }
/** * @param $index * @param null $output */ public function resetIndex($index, $output = null) { $indexObj = $this->indexRegistry->getIndex($index); $config = $this->configManager->getIndexConfiguration($index)->getSettings(); if (!$indexObj->exists()) { $indexObj->create($config); } else { $indexObj->close(); $indexObj->setSettings($config); $indexObj->open(); } }
/** * @param $index * @param $type * @return array * @throws \Exception */ protected function getTypesToPopulate($index, $type = null) { $indexObj = $this->indexRegistry->getIndex($index); if (!$indexObj) { throw new \Exception("The index {$index} is not defined"); } // creating index if not exists if (!$indexObj->exists()) { $this->resetter->resetIndex($index); } $typesConfig = $this->configManager->getIndexConfiguration($index)->getTypes(); if ($type) { if (!isset($typesConfig[$type])) { throw new \Exception("The type {$type} is not defined in this index."); } $typeConfig = $typesConfig[$type]; $types = array($type => $indexObj->getType($typeConfig->getType())); } else { $this->resetter->reset($index); $types = array_combine(array_keys($typesConfig), array_map(function (TypeConfig $typeConfig) use($indexObj) { return $indexObj->getType($typeConfig->getType()); }, $typesConfig)); } return $types; }
/** * Update the elasticsearch document * @param string $uri * @param array $types * @param string $index * @param FiliationBuilder $filiationBuilder * @param ResourceToDocumentTransformer $resourceToDocumentTransformer */ public function updateDocument($uri, $types, $filiationBuilder, ResourceToDocumentTransformer $resourceToDocumentTransformer) { // find the finest type of the resource in order to index the resource ony once $typeName = $filiationBuilder->getMostAccurateType($types, $this->serializerHelper->getAllTypes()); // not specified in project ontology description if ($typeName === null) { throw new \Exception('No type found to update the ES document ' . $uri); } else { if (count($typeName) == 1) { $typeName = $typeName[0]; } else { throw new \Exception("The most accurate type for " . $uri . " has not be found."); } } $typesConfig = $this->configManager->getTypesConfigurationByClass($typeName); foreach ($typesConfig as $typeConfig) { $indexConfig = $typeConfig->getIndex(); $index = $indexConfig->getName(); $esType = $this->indexRegistry->getIndex($index)->getType($typeConfig->getType()); $document = $resourceToDocumentTransformer->transform($uri, $index, $typeName); if ($document) { $esType->addDocument($document); } } }
/** * If a types resource has changed and the new type is mapped to another document type, then the old document is removed * @param $uri * @param $types * @param $oldType * @return bool */ protected function deleteOldDocument($uri, $types, $oldType) { $newTypes = array(); foreach ($types as $type) { $type = (string) $type; if ($type && !empty($type)) { $newTypes[] = RdfNamespace::shorten($type); } } // Get most accurtype of oldtype $mostAccurateTypes = $this->filiationBuilder->getMostAccurateType(array(RdfNamespace::expand($oldType)), $this->serializerHelper->getAllTypes()); $mostAccurateType = null; // not specified in project ontology description if (count($mostAccurateTypes) == 1) { $mostAccurateType = $mostAccurateTypes[0]; } else { // echo "Seems to not have to be indexed"; } if (!in_array($mostAccurateType, $newTypes)) { $typesConfig = $this->configManager->getTypesConfigurationByClass($mostAccurateType); foreach ($typesConfig as $typeConfig) { $indexConfig = $typeConfig->getIndex(); $index = $indexConfig->getName(); $this->container->get('nemrod.elastica.jsonld.frame.loader')->setEsIndex($index); if ($index !== null) { $esType = $this->indexRegistry->getIndex($index)->getType($typeConfig->getType()); // Trow an exeption if document does not exist try { $esType->deleteDocument(new Document($uri, array(), $mostAccurateType, $indexConfig->getElasticSearchName())); return true; } catch (\Exception $e) { } } } } return false; }