Elastica\Document::addFile PHP Method

addFile() public method

To use this feature you have to call the following command in the elasticsearch directory: ./bin/plugin -install elasticsearch/elasticsearch-mapper-attachments/1.6.0 This installs the tika file analysis plugin. More infos about supported formats can be found here: {@link http://tika.apache.org/0.7/formats.html}
public addFile ( string $key, string $filepath, string $mimeType = '' )
$key string Key to add the file to
$filepath string Path to add the file
$mimeType string OPTIONAL Header mime type
    public function addFile($key, $filepath, $mimeType = '')
    {
        $value = base64_encode(file_get_contents($filepath));
        if (!empty($mimeType)) {
            $value = ['_content_type' => $mimeType, '_name' => $filepath, '_content' => $value];
        }
        $this->set($key, $value);
        return $this;
    }

Usage Example

 /**
  * 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;
 }
All Usage Examples Of Elastica\Document::addFile