/** * Build and return a document from the data source, ready for insertion into ES * * @param int|string $id * @return array */ public function getDocument($id) { $params = ['index' => $this->sourceIndexManager->getLiveIndex(), 'type' => $this->metadataCollector->getDocumentMetadata($this->sourceDocumentClass)->getType(), 'id' => $id]; $doc = $this->sourceIndexManager->getConnection()->getClient()->get($params); $result = $doc['_source']; $result['_id'] = $doc['_id']; return $result; }
/** * Adds a prepared document array to a bulk request for the next commit. * Depending on the connection autocommit mode, the update may be committed right away. * * @param string $documentClass The document class in short notation (i.e. AppBundle:Product) * @param array $documentArray The document to index in ES */ public function persistRaw($documentClass, array $documentArray) { $documentMetadata = $this->metadataCollector->getDocumentMetadata($documentClass); $this->getConnection()->addBulkOperation('index', $this->writeAlias, $documentMetadata->getType(), $documentArray); if ($this->getConnection()->isAutocommit()) { $this->getConnection()->commit(); } }
/** * Verify that all types are in indices using the same connection object and return that object * * @param array $documentClasses * @return ConnectionManager */ private function getConnection(array $documentClasses) { $connection = null; foreach ($documentClasses as $documentClass) { $indexManagerName = $this->documentMetadataCollector->getDocumentClassIndex($documentClass); $indexManager = $this->indexManagerRegistry->get($indexManagerName); if (!is_null($connection) && $indexManager->getConnection()->getConnectionName() != $connection->getConnectionName()) { throw new \InvalidArgumentException(sprintf('All searched types must be in indices within the same connection')); } $connection = $indexManager->getConnection(); } return $connection; }
/** * Converts document or (nested) object to an array. * * @param ObjectInterface $object A document or a (nested) object * @param array $propertiesMetadata * * @return array */ public function convertToArray(ObjectInterface $object, $propertiesMetadata = []) { if (empty($propertiesMetadata)) { $propertiesMetadata = $this->metadataCollector->getObjectPropertiesMetadata(get_class($object)); } $array = []; foreach ($propertiesMetadata as $name => $propertyMetadata) { if ($propertyMetadata['propertyAccess'] == DocumentMetadata::PROPERTY_ACCESS_PRIVATE) { $value = $object->{$propertyMetadata['methods']['getter']}(); } else { $value = $object->{$propertyMetadata['propertyName']}; } if (isset($value)) { // If this is a (nested) object or a list of such if (array_key_exists('propertiesMetadata', $propertyMetadata)) { $new = []; if ($propertyMetadata['multiple']) { // Verify value is traversable if (!(is_array($value) || is_object($value) && $value instanceof \Traversable)) { throw new \InvalidArgumentException(sprintf('Value of "%s" is not traversable, although field is set to "multiple"')); } foreach ($value as $item) { $this->checkObjectType($item, $propertyMetadata['className']); $new[] = $this->convertToArray($item, $propertyMetadata['propertiesMetadata']); } } else { $this->checkObjectType($value, $propertyMetadata['className']); $new = $this->convertToArray($value, $propertyMetadata['propertiesMetadata']); } $array[$name] = $new; } elseif ($value instanceof MLProperty) { foreach ($value->getValues() as $language => $langValue) { $array[$name . $this->languageSeparator . $language] = $langValue; } } else { $array[$name] = $value; } } } return $array; }
/** * Test getting metadata for document class */ public function testGetDocumentMetadata() { $this->assertInstanceOf('Sineflow\\ElasticsearchBundle\\Mapping\\DocumentMetadata', $this->metadataCollector->getDocumentMetadata('TestBundle:Foo'), 'Incorrect metadata.'); }
/** * Returns the index manager managing a given Elasticsearch entity * * @param DocumentInterface $entity * @return IndexManager */ public function getByEntity(DocumentInterface $entity) { $indexManagerName = $this->metadataCollector->getDocumentClassIndex(get_class($entity)); return $this->get($indexManagerName); }