setData() public method

Overwrites the current document data with the given data.
public setData ( array | string $data )
$data array | string Data array
Example #1
0
 /**
  * Convert a log message into an Elastica Document
  *
  * @param  array $record Log message
  * @return Document
  */
 protected function getDocument($record)
 {
     $document = new Document();
     $document->setData($record);
     $document->setType($this->type);
     $document->setIndex($this->index);
     return $document;
 }
Example #2
0
 public function getDocument($record)
 {
     $user = $this->token->getToken()->getUser();
     $record['extra']['user'] = $user->getId();
     $document = new Document();
     $document->setData($record);
     $document->setType($this->type);
     $document->setIndex($this->index);
     return $document;
 }
 /**
  * Partial Update
  *
  * @param  string  $docId
  * @param  array    $data
  * @param  integer  $version
  *
  * @throws IndexingException
  *
  * @return null
  */
 public function update($docId, array $data, $version = 1)
 {
     $document = new Document();
     $document->setData($data);
     $document->setId($docId);
     $document->setDocAsUpsert(true);
     try {
         $this->getType($version)->updateDocument($document);
     } catch (\Exception $e) {
         throw new IndexingException('Throw exception while updating', $e->getCode(), $e);
     }
 }
 /**
  * Insert the repository objects in the type index
  *
  * @param \Closure $loggerClosure
  * @param array    $options
  */
 public function populate(\Closure $loggerClosure = null, array $options = array())
 {
     if ($loggerClosure) {
         $loggerClosure('Indexing movies');
     }
     $allMovies = $this->movieManager->getAllMovies();
     $languages = $this->getLanguagesAvailable();
     foreach ($allMovies as $movie) {
         $document = new Document();
         $id = $movie->getId();
         $document->setId($id);
         $titleFr = $this->movieManager->getMovieTitleInLocale($id, $languages['fr']);
         $titleEn = $this->movieManager->getMovieTitleInLocale($id, $languages['en']);
         //            $titleEn = $this->movieManager->getMovieTitleInLocale($id, 'en');
         //            $titleFr = $this->movieManager->getMovieTitleInLocale($id, 'fr');
         $document->setData(array('id' => $id, 'title_fr' => $titleFr['title'], 'title_en' => $titleEn['title']));
         $this->movieType->addDocuments(array($document));
     }
 }
 /**
  * {@inheritdoc}
  * @see \Silex\ServiceProviderInterface::register()
  */
 public function register(Application $app)
 {
     $app['elastic.client'] = $app->share(function () use($app) {
         $config = $app['config']('elastic.connection', array());
         $client = new Client($config);
         return $client;
     });
     $app['elastic.bulk'] = $app->protect(function ($index) use($app) {
         $bulk = new Bulk($app['elastic.client']);
         $bulk->setIndex($index);
         return $bulk;
     });
     $app['elastic.document'] = $app->protect(function ($type, array $data) {
         $document = new Document();
         $document->setType($type);
         $document->setData($data);
         return $document;
     });
 }
Example #6
0
 /**
  * @param \Elastica\Response $response
  * @param \Elastica\Document $document
  * @param string             $fields   Array of field names to be populated or '_source' if whole document data should be updated
  */
 protected function _populateDocumentFieldsFromResponse(Response $response, Document $document, $fields)
 {
     $responseData = $response->getData();
     if ('_source' == $fields) {
         if (isset($responseData['get']['_source']) && is_array($responseData['get']['_source'])) {
             $document->setData($responseData['get']['_source']);
         }
     } else {
         $keys = explode(',', $fields);
         $data = $document->getData();
         foreach ($keys as $key) {
             if (isset($responseData['get']['fields'][$key])) {
                 $data[$key] = $responseData['get']['fields'][$key];
             } elseif (isset($data[$key])) {
                 unset($data[$key]);
             }
         }
         $document->setData($data);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function logStringQuery($searchTerm, $ipAddress)
 {
     $document = new Document();
     $document->setData(array('search_term' => $searchTerm, 'ip_address' => $ipAddress));
     $this->type->addDocuments(array($document));
 }
Example #8
0
 /**
  * Uses _bulk to send documents to the server
  *
  * @param objects[] $objects
  * @return \Elastica\Bulk\ResponseSet
  * @link http://www.elasticsearch.org/guide/reference/api/bulk.html
  */
 public function addObjects(array $objects)
 {
     if (!isset($this->_serializer)) {
         throw new RuntimeException('No serializer defined');
     }
     $docs = array();
     foreach ($objects as $object) {
         $data = call_user_func($this->_serializer, $object);
         $doc = new Document();
         $doc->setData($data);
         $doc->setType($this->getName());
         $docs[] = $doc;
     }
     return $this->getIndex()->addDocuments($docs);
 }
Example #9
0
 /**
  * @group unit
  */
 public function testUpsert()
 {
     $document = new Document();
     $upsert = new Document();
     $upsert->setData(array('someproperty' => 'somevalue'));
     $this->assertFalse($document->hasUpsert());
     $document->setUpsert($upsert);
     $this->assertTrue($document->hasUpsert());
     $this->assertSame($upsert, $document->getUpsert());
 }
Example #10
0
 public function testSetData()
 {
     $doc = new Document();
     $returnValue = $doc->setData(array('data'));
     $this->assertInstanceOf('Elastica\\Document', $returnValue);
 }
Example #11
0
 public function addObject($object, Document $doc = null)
 {
     if (!isset($this->_serializer)) {
         throw new RuntimeException('No serializer defined');
     }
     $data = call_user_func($this->_serializer, $object);
     if (!$doc) {
         $doc = new Document();
     }
     $doc->setData($data);
     return $this->addDocument($doc);
 }