/** * Construct an entity object * * @param ClassMetadata $class * @param object $document */ public function hydrateEntity(ClassMetadata $class, $document) { // TODO: add support for different result set types from different clients // perhaps by wrapping documents in a layer of abstraction $data = $document->getData(); $fields = array_merge($document->hasFields() ? $document->getFields() : array(), array('_version' => $document->getVersion())); foreach ($fields as $name => $value) { if (isset($class->parameters[$name])) { $data[$name] = $value; } else { foreach ($class->parameters as $param => $mapping) { if ($mapping->name == $name) { $data[$param] = $value; break; } } } } $data[$class->getIdentifier()] = $document->getId(); if (method_exists($document, 'getScore')) { $data['score'] = $document->getScore(); } $entity = $this->sm->getSerializer()->deserialize($class->className, json_encode($data)); if ($this->evm->hasListeners(Events::postLoad)) { $this->evm->dispatchEvent(Events::postLoad, new Event\LifecycleEventArgs($entity, $this->sm)); } return $entity; }
/** * Execute a direct delete on the associated index and type * * @param object $query */ public function delete($query) { $classes = $this->getClassMetadata(); foreach ($classes as $class) { $this->_sm->getClient()->removeAll($class, $query); } }
/** * Delete all indices */ public function deleteAllIndices() { /** @var ClassMetadata[] $metadatas */ $metadatas = $this->sm->getMetadataFactory()->getAllMetadata(); foreach ($metadatas as $metadata) { try { $this->client->deleteIndex($metadata->getIndexForRead()); } catch (ResponseException $e) { if (strpos($e->getResponse()->getError(), 'IndexMissingException') === false) { // The original error from ES is not "IndexMissingException". We shouldn't swallow it. throw $e; } // The index has been deleted already, skip it. } } }
/** * Execute search and hydrate results if required. * * @param integer $hydrationMode * @throws DoctrineSearchException * @return mixed */ public function getResult($hydrationMode = null) { if ($hydrationMode) { $this->hydrationMode = $hydrationMode; } $classes = array(); foreach ($this->entityClasses as $entityClass) { $classes[] = $this->sm->getClassMetadata($entityClass); } $resultSet = $this->getSearchManager()->getClient()->search($this->query, $classes); // TODO: abstraction of support for different result sets if ($resultSet instanceof Elastica\ResultSet) { $this->count = $resultSet->getTotalHits(); $this->facets = $resultSet->getFacets(); $results = $resultSet->getResults(); } else { $resultClass = get_class($resultSet); throw new DoctrineSearchException("Unexpected result set class '{$resultClass}'"); } // If having the hydration query set, hydrate using the hydrate query if ($this->getHydrationQuery()) { // Document ids are used to lookup dbms results $fn = function ($result) { return $result->getId(); }; $ids = array_map($fn, $results); return $this->getHydrationQuery()->setParameter($this->hydrationParameter, $ids ?: null)->useResultCache($this->useResultCache, $this->cacheLifetime)->getResult($this->hydrationMode); } // Return results depending on hydration mode switch ($this->hydrationMode) { case self::HYDRATE_BYPASS: return $resultSet; case self::HYDRATE_AGGREGATION: return $resultSet->getAggregations(); } return $this->sm->getUnitOfWork()->hydrateCollection($classes, $resultSet); }
/** * @return \Revinate\SearchBundle\Lib\Search\SearchClientInterface */ public function getClient() { return $this->searchManager->getClient(); }
public function testGetClassMetadataFactory() { $mdf = $this->sm->getClassMetadataFactory(); $this->assertInstanceOf('Revinate\\SearchBundle\\Lib\\Search\\Mapping\\ClassMetadataFactory', $mdf); }
/** * @param BaseElasticsearchEntity $entity * @param bool $refresh * @param bool $forceVersion * * @throws \Exception */ public function save($entity, $refresh = false, $forceVersion = false) { $this->_sm->persist($entity); $this->_sm->flush($entity, $refresh, $forceVersion); }
/** * {@inheritDoc} */ protected function initialize() { $this->driver = $this->config->getMetadataDriverImpl(); $this->evm = $this->sm->getEventManager(); $this->initialized = true; }