/** * @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 $type * * @return array * * @throws \Exception */ public function buildMapping($index, $type) { $typeConfig = $this->configManager->getTypeConfiguration($index, $type); $mappingData = $typeConfig->getProperties(); if (!$mappingData) { throw new \Exception('no mapping for type'); } /** @var Type $typeObj */ $typeObj = $this->indexRegistry->getIndex($index)->getType($typeConfig->getType()); try { $typeObj->delete(); } catch (ResponseException $e) { } $typeObj->setMapping($mappingData); return $typeObj->getMapping(); }
/** * @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); } } }
/** * Transform a resource to an elastica document. * * @param $uri * @param $index * @param $type * * @return Document|null */ public function transform($uri, $index, $type) { if ($index && $this->serializerHelper->isTypeIndexed($index, $type)) { $frame = $this->serializerHelper->getTypeFramePath($index, $type); $phpClass = TypeMapper::get($type); if (!$phpClass) { $phpClass = "EasyRdf\\Resource"; } $jsonLd = $this->jsonLdSerializer->serialize(new $phpClass($uri), $frame, array("includeParentClassFrame" => true)); $graph = json_decode($jsonLd, true); if (!isset($graph['@graph'][0])) { return; } $resource = $graph['@graph'][0]; $resource['@id'] = $uri; $json = json_encode($resource); $json = str_replace('@id', '_id', $json); $json = str_replace('@type', '_type', $json); $index = $this->configManager->getIndexConfiguration($index)->getElasticSearchName(); return new Document($uri, $json, $type, $index); } return; }
/** * @throws \Exception */ protected function cascadeRemoveDocuments() { // cascade remove foreach ($this->arrayResourcesToUpdateAfterDeletion as $uri => $type) { $typesConfig = $this->configManager->getTypesConfigurationByClass($type); foreach ($typesConfig as $typeConfig) { $indexConfig = $typeConfig->getIndex(); $index = $indexConfig->getName(); $this->container->get('nemrod.elastica.jsonld.frame.loader')->setEsIndex($index); if ($index !== null) { $this->cascadeUpdateHelper->updateDocument($uri, array($type), $this->filiationBuilder, $this->resourceToDocumentTransformer); } } } }