Add file content.
public addFileContent ( string $key, string $content ) | ||
$key | string | Document key |
$content | string | Raw file content |
/** * Transforms an object into an elastica object having the required keys * * @param object $object the object to convert * @param array $fields the keys we want to have in the returned array * * @return Document **/ public function transform($object, array $fields) { $identifier = $this->propertyAccessor->getValue($object, $this->options['identifier']); $document = new Document($identifier); foreach ($fields as $key => $mapping) { if ($key == '_parent') { $property = null !== $mapping['property'] ? $mapping['property'] : $mapping['type']; $value = $this->propertyAccessor->getValue($object, $property); $document->setParent($this->propertyAccessor->getValue($value, $mapping['identifier'])); continue; } $value = $this->propertyAccessor->getValue($object, $key); if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object')) && isset($mapping['properties']) && !empty($mapping['properties'])) { /* $value is a nested document or object. Transform $value into * an array of documents, respective the mapped properties. */ $document->set($key, $this->transformNested($value, $mapping['properties'])); continue; } if (isset($mapping['type']) && $mapping['type'] == 'attachment') { // $value is an attachment. Add it to the document. if ($value instanceof \SplFileInfo) { $document->addFile($key, $value->getPathName()); } else { $document->addFileContent($key, $value); } continue; } $document->set($key, $this->normalizeValue($value)); } return $document; }
public function testAddPdfFileContent() { $indexMapping = array('file' => array('type' => 'attachment', 'store' => 'no'), 'text' => array('type' => 'string', 'store' => 'no')); $indexParams = array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)); $index = $this->_createIndex(); $type = new Type($index, 'test'); $index->create($indexParams, true); $type->setMapping($indexMapping); $doc1 = new Document(1); $doc1->addFileContent('file', file_get_contents(BASE_PATH . '/data/test.pdf')); $doc1->set('text', 'basel world'); $type->addDocument($doc1); $doc2 = new Document(2); $doc2->set('text', 'running in basel'); $type->addDocument($doc2); $index->optimize(); $resultSet = $type->search('xodoa'); $this->assertEquals(1, $resultSet->count()); $resultSet = $type->search('basel'); $this->assertEquals(2, $resultSet->count()); // Author is ruflin $resultSet = $type->search('ruflin'); $this->assertEquals(1, $resultSet->count()); // String does not exist in file $resultSet = $type->search('guschti'); $this->assertEquals(0, $resultSet->count()); }
/** * Transforms the given object to an elastica document * * @param object $object the object to convert * @param array $fields the keys we want to have in the returned array * @param string $identifier the identifier for the new document * @return Document */ protected function transformObjectToDocument($object, array $fields, $identifier = '') { $document = new Document($identifier); foreach ($fields as $key => $mapping) { if ($key == '_parent') { $property = null !== $mapping['property'] ? $mapping['property'] : $mapping['type']; $value = $this->propertyAccessor->getValue($object, $property); $document->setParent($this->propertyAccessor->getValue($value, $mapping['identifier'])); continue; } $path = isset($mapping['property_path']) ? $mapping['property_path'] : $key; if (false === $path) { continue; } $value = $this->propertyAccessor->getValue($object, $path); if (isset($mapping['type']) && in_array($mapping['type'], array('nested', 'object')) && isset($mapping['properties']) && !empty($mapping['properties'])) { /* $value is a nested document or object. Transform $value into * an array of documents, respective the mapped properties. */ $document->set($key, $this->transformNested($value, $mapping['properties'])); continue; } if (isset($mapping['type']) && $mapping['type'] == 'attachment') { // $value is an attachment. Add it to the document. if ($value instanceof \SplFileInfo) { $document->addFile($key, $value->getPathName()); } else { $document->addFileContent($key, $value); } continue; } $document->set($key, $this->normalizeValue($value)); } if ($this->dispatcher) { $event = new TransformEvent($document, $fields, $object); $this->dispatcher->dispatch(TransformEvent::POST_TRANSFORM, $event); $document = $event->getDocument(); } return $document; }