set() public method

public set ( string $key, mixed $value )
$key string
$value mixed
Ejemplo n.º 1
0
 /**
  * {@inheritDoc}
  */
 public function toDocument(Model $model, Document $document)
 {
     $mapping = ['city' => 'city', 'state' => 'state', 'country' => 'country', 'zip_code' => 'zip_code'];
     foreach ($mapping as $field => $key) {
         $document->set($key, $model->{$field});
     }
     if (!empty($model->lat) && !empty($model->lon)) {
         $document->set('location', ['lon' => $model->lon, 'lat' => $model->lat]);
     }
     $fullAddress = [];
     if (!empty($model->city)) {
         $fullAddress[] = $model->city;
     }
     if (!empty($model->state)) {
         $fullAddress[] = $model->state;
     }
     if (!empty($model->zip_code)) {
         $fullAddress[] = $model->zip_code;
     }
     if (!empty($model->country)) {
         $fullAddress[] = $model->country;
     }
     $document->set('full_address', implode(', ', $fullAddress));
     return $document;
 }
 /**
  * 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;
 }
 /**
  * Populate elastica with the location of the photograph
  * @param  \Elastica\Document $document Representation of an Elastic Search document
  * @return \Elastica\Document modified version of the document
  */
 public function updateElasticsearchDocument(\Elastica\Document $document)
 {
     //	self::$ctr++;
     $coors = array('lat' => $this->owner->Lat, 'lon' => $this->owner->Lon);
     $document->set('location', $coors);
     $sortable = $this->owner->ShutterSpeed;
     $sortable = explode('/', $sortable);
     if (sizeof($sortable) == 1) {
         $sortable = trim($sortable[0]);
         if ($this->owner->ShutterSpeed == null) {
             $sortable = null;
         }
         if ($sortable === '1') {
             $sortable = '1.000000';
         }
     } else {
         if (sizeof($sortable) == 2) {
             $sortable = floatval($sortable[0]) / intval($sortable[1]);
             $sortable = round($sortable, 6);
         }
     }
     $sortable = $sortable . '|' . $this->owner->ShutterSpeed;
     $document->set('ShutterSpeed', $sortable);
     return $document;
 }
 /**
  * @param Document $document
  * @param Content $content
  */
 public function indexExtraFields(Document $document, Content $content)
 {
     if (!$content instanceof EntityContent || $content->isRedirect() === true) {
         return;
     }
     $fields = $this->fieldDefinitions->getFields();
     $entity = $content->getEntity();
     foreach ($fields as $fieldName => $field) {
         $data = $field->getFieldData($entity);
         $document->set($fieldName, $data);
     }
 }
Ejemplo n.º 5
0
 /**
  * {@inheritDoc}
  */
 public function toDocument(Model $model, Document $document)
 {
     foreach ($this->mapping as $field => $key) {
         $document->set($key, $model->{$field});
     }
     if (!empty($model->job_ziplat) && !empty($model->job_ziplon)) {
         $document->set('location', $model->job_ziplat . ',' . $model->job_ziplon);
     }
     $fullAddress = [];
     if (!empty($model->job_address)) {
         $fullAddress[] = $model->job_address;
     }
     if (!empty($model->city)) {
         $fullAddress[] = $model->city;
     }
     if (!empty($model->job_postcode)) {
         $fullAddress[] = $model->job_postcode;
     }
     $document->set('full_address', implode(', ', $fullAddress));
     return $document;
 }
Ejemplo n.º 6
0
 public function testMatchDoc()
 {
     $client = new Client(array('persistent' => false));
     $index = $client->getIndex('elastica_test');
     $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true);
     $percolator = new Percolator($index);
     $percolatorName = 'percotest';
     $query = new Term(array('name' => 'ruflin'));
     $response = $percolator->registerQuery($percolatorName, $query);
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $doc1 = new Document();
     $doc1->set('name', 'ruflin');
     $doc2 = new Document();
     $doc2->set('name', 'nicolas');
     $index = new Index($index->getClient(), '_percolator');
     $index->optimize();
     $index->refresh();
     $matches1 = $percolator->matchDoc($doc1);
     $this->assertTrue(in_array($percolatorName, $matches1));
     $this->assertEquals(1, count($matches1));
     $matches2 = $percolator->matchDoc($doc2);
     $this->assertEmpty($matches2);
 }
Ejemplo n.º 7
0
 public function testAddDocumentVersion()
 {
     $client = $this->_getClient();
     $index = $client->getIndex('test');
     $index->create(array(), true);
     $type = new Type($index, 'test');
     $doc1 = new Document(1);
     $doc1->set('title', 'Hello world');
     $return = $type->addDocument($doc1);
     $data = $return->getData();
     $this->assertEquals(1, $data['_version']);
     $return = $type->addDocument($doc1);
     $data = $return->getData();
     $this->assertEquals(2, $data['_version']);
 }
Ejemplo n.º 8
0
 /**
  * @group unit
  */
 public function testSerializedData()
 {
     $data = '{"user":"******"}';
     $document = new Document(1, $data);
     $this->assertFalse($document->has('user'));
     try {
         $document->get('user');
         $this->fail('User field should not be available');
     } catch (InvalidException $e) {
         $this->assertTrue(true);
     }
     try {
         $document->remove('user');
         $this->fail('User field should not be available for removal');
     } catch (InvalidException $e) {
         $this->assertTrue(true);
     }
     try {
         $document->set('name', 'shawn');
         $this->fail('Document should not allow to set new data');
     } catch (InvalidException $e) {
         $this->assertTrue(true);
     }
 }
 /**
  * Test case for using filtered percolator queries based on the Elasticsearch documentation examples.
  */
 public function testRegisterAndUnregisterPercolator()
 {
     // step one: register create index and setup the percolator query from the ES documentation.
     $index = $this->_createIndex();
     $percolator = new Percolator($index);
     $baseQuery = new Term(array('field1' => 'value1'));
     $fields = array('color' => 'blue');
     $response = $percolator->registerQuery('kuku', $baseQuery, $fields);
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     // refreshing is required in order to ensure the query is really ready for execution.
     $index->refresh();
     // step two: match a document which should match the kuku query when filtered on the blue color
     $doc = new Document();
     $doc->set('field1', 'value1');
     $matches = $percolator->matchDoc($doc, new Term(array('color' => 'blue')));
     $this->assertCount(1, $matches, 'No or too much registered query matched.');
     $this->assertEquals('kuku', $matches[0]['_id'], 'A wrong registered query has matched.');
     // step three: validate that using a different color, no registered query matches.
     $matches = $percolator->matchDoc($doc, new Term(array('color' => 'green')));
     $this->assertCount(0, $matches, 'A registered query matched, although nothing should match at all.');
     // unregister percolator query
     $response = $percolator->unregisterQuery('kuku');
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     // refreshing is required in order to ensure the query is really ready for execution.
     $index->refresh();
     $matches = $percolator->matchDoc($doc, new Term(array('color' => 'blue')));
     $this->assertCount(0, $matches, 'Percolator query did not get deleted.');
     $index->delete();
 }
Ejemplo n.º 10
0
 /**
  * @group functional
  */
 public function testAddDocumentAutoGeneratedId()
 {
     $index = $this->_createIndex();
     $type = $index->getType('elastica_type');
     $document = new Document();
     $document->setAutoPopulate();
     $document->set('name', 'ruflin');
     $this->assertEquals('', $document->getId());
     $this->assertFalse($document->hasId());
     $type->addDocument($document);
     $this->assertNotEquals('', $document->getId());
     $this->assertTrue($document->hasId());
     $foundDoc = $type->getDocument($document->getId());
     $this->assertInstanceOf('Elastica\\Document', $foundDoc);
     $this->assertEquals($document->getId(), $foundDoc->getId());
     $data = $foundDoc->getData();
     $this->assertArrayHasKey('name', $data);
     $this->assertEquals('ruflin', $data['name']);
 }
 /**
  * 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;
 }
Ejemplo n.º 12
0
 /**
  * @group functional
  */
 public function testPercolateWithAdditionalRequestBodyOptions()
 {
     $index = $this->_createIndex();
     $percolator = new Percolator($index);
     $query = new Term(array('name' => 'foo'));
     $response = $percolator->registerQuery('percotest', $query, array('field1' => array('tag1', 'tag2')));
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $query = new Term(array('name' => 'foo'));
     $response = $percolator->registerQuery('percotest1', $query, array('field1' => array('tag2')));
     $this->assertTrue($response->isOk());
     $this->assertFalse($response->hasError());
     $doc1 = new Document();
     $doc1->set('name', 'foo');
     $index->refresh();
     $options = array('track_scores' => true, 'sort' => array('_score' => 'desc'), 'size' => 1);
     $matches = $percolator->matchDoc($doc1, new Term(array('field1' => 'tag2')), 'type', $options);
     $this->assertCount(1, $matches);
     $this->assertEquals('percotest1', $matches[0]['_id']);
     $this->assertArrayHasKey('_score', $matches[0]);
 }
Ejemplo n.º 13
0
 /**
  * add a document to the list. To index all the list execute flushDocuments
  * 
  * @param   \Elastica\Document   $document
  * */
 protected function pushDocument($document)
 {
     // Get language if exists
     $lang = $document->__isset('language') ? $document->language : '*';
     //Add boost field if does not exist
     if (!$document->__isset('boost') && $this->boost) {
         $document->set('boost', $this->boost);
     }
     // Extract the first letter of the langue (en for en-GB)
     $explode = explode('-', $lang);
     $lang = $explode[0];
     // If it is the first of this language
     if (!array_key_exists($lang, $this->documents)) {
         $this->documents[$lang] = array();
         // Init array
     }
     // add document to the list
     $this->documents[$lang][] = $document;
     $mem_limit_bytes = trim(ini_get('memory_limit')) * 1024 * 1024;
     if (memory_get_usage() > $mem_limit_bytes * 0.2) {
         // Check memory use
         //if documents array is too big we flush it
         $this->flushDocuments();
     }
 }
Ejemplo n.º 14
0
 /**
  * @param $config
  * @param $fieldName
  * @param \Elastica\Document $document
  * @param $fieldValue
  */
 public function setValue($config, $fieldName, $document, $fieldValue)
 {
     switch ($config['type']) {
         case 'date':
             if ($fieldValue) {
                 $document->set($fieldName, $this->formatDate($fieldValue));
             }
             break;
         default:
             $document->set($fieldName, $fieldValue);
             break;
     }
 }