/** * Adds document to a bulk request for the next flush. * * @param DocumentInterface $document The document entity to index in ES */ public function persist(DocumentInterface $document) { $this->dispatchEvent(Events::PRE_PERSIST, new ElasticsearchPersistEvent($this->getConnection(), $document)); $documentClass = get_class($document); $documentMetadata = $this->metadataCollection->getDocumentMetadata($documentClass); $converter = isset($this->convertersCache[$documentClass]) ? $this->convertersCache[$documentClass] : new Converter($documentMetadata, $this->languageSeparator); $documentArray = $converter->convertToArray($document); $this->persistRaw($documentMetadata->getType(), $documentArray); $this->dispatchEvent(Events::POST_PERSIST, new ElasticsearchPersistEvent($this->getConnection(), $document)); }
/** * Returns a mapping of live indices and types to the document classes in short notation that represent them * * @param string[] $documentClasses * @return IndicesAndTypesMetadataCollection */ private function getIndicesAndTypesMetadataCollection(array $documentClasses) { $allDocumentClassToIndexMappings = $this->documentMetadataCollection->getDocumentClassesIndices(); $documentClassToIndexMap = array_intersect_key($allDocumentClassToIndexMappings, array_flip($documentClasses)); $typesMetadataCollection = new IndicesAndTypesMetadataCollection(); $getLiveIndices = false; $classToTypeMap = $this->documentMetadataCollection->getClassToTypeMap($documentClasses); // If there are duplicate type names across the indices we're querying if (count($classToTypeMap) > count(array_unique($classToTypeMap))) { // We'll need to get the live index name for each type, so we can correctly map the results to the appropriate objects $getLiveIndices = true; } foreach ($documentClassToIndexMap as $documentClass => $indexManagerName) { // Build mappings of indices and types to metadata, for the Converter $liveIndex = $getLiveIndices ? $this->indexManagerFactory->get($indexManagerName)->getLiveIndex() : null; $documentMetadata = $this->documentMetadataCollection->getDocumentMetadata($documentClass); $typesMetadataCollection->setTypeMetadata($documentMetadata, $liveIndex); } return $typesMetadataCollection; }
/** * @return DocumentMetadata The metadata for the ES type the provider is for */ protected function getDocumentMetadata() { return $this->metadataCollection->getDocumentMetadata($this->documentClass); }