Example #1
0
 /**
  * @group unit
  */
 public function testSetScript()
 {
     $script = new Script('foo');
     $this->assertEquals('foo', $script->getScript());
     $script->setScript('bar');
     $this->assertEquals('bar', $script->getScript());
     $this->assertInstanceOf('Elastica\\Script\\Script', $script->setScript('foo'));
 }
Example #2
0
 /**
  * Sets script object.
  *
  * @param \Elastica\Script\Script|string|array $script
  *
  * @return $this
  */
 public function setScript($script)
 {
     return $this->setParam('script', Elastica\Script\Script::create($script));
 }
Example #3
0
 /**
  * @param string     $scriptFile
  * @param array|null $params
  * @param null       $id
  */
 public function __construct($scriptFile, array $params = null, $id = null)
 {
     parent::__construct(null, $params, null, $id);
     $this->setScriptFile($scriptFile);
 }
Example #4
0
 /**
  * @group functional
  */
 public function testUpdateDocumentPopulateFields()
 {
     $this->_checkScriptInlineSetting();
     $index = $this->_createIndex();
     $type = $index->getType('test');
     $client = $index->getClient();
     $newDocument = new Document(1, array('field1' => 'value1', 'field2' => 10, 'field3' => 'should be removed', 'field4' => 'value4'));
     $newDocument->setAutoPopulate();
     $type->addDocument($newDocument);
     $script = new Script('ctx._source.field2 += count; ctx._source.remove("field3"); ctx._source.field4 = "changed"');
     $script->setParam('count', 5);
     $script->setUpsert($newDocument);
     $client->updateDocument(1, $script, $index->getName(), $type->getName(), array('fields' => '_source'));
     $data = $type->getDocument(1)->getData();
     $this->assertArrayHasKey('field1', $data);
     $this->assertEquals('value1', $data['field1']);
     $this->assertArrayHasKey('field2', $data);
     $this->assertEquals(15, $data['field2']);
     $this->assertArrayHasKey('field4', $data);
     $this->assertEquals('changed', $data['field4']);
     $this->assertArrayNotHasKey('field3', $data);
     $script = new Script('ctx._source.field2 += count; ctx._source.remove("field4"); ctx._source.field1 = field1;');
     $script->setParam('count', 5);
     $script->setParam('field1', 'updated');
     $script->setUpsert($newDocument);
     $client->updateDocument(1, $script, $index->getName(), $type->getName(), array('fields' => 'field2,field4'));
     $document = $type->getDocument(1);
     $data = $document->getData();
     $this->assertArrayHasKey('field1', $data);
     $this->assertEquals('updated', $data['field1']);
     $this->assertArrayHasKey('field2', $data);
     $this->assertEquals(20, $data['field2']);
     $this->assertArrayNotHasKey('field3', $data);
     $this->assertArrayNotHasKey('field4', $data);
 }
Example #5
0
 /**
  * @group functional
  */
 public function testUpdateDocumentWithoutSource()
 {
     $index = $this->_createIndex();
     $type = $index->getType('elastica_type');
     $mapping = new Mapping();
     $mapping->setProperties(array('name' => array('type' => 'string', 'store' => 'yes'), 'counter' => array('type' => 'integer', 'store' => 'no')));
     $mapping->disableSource();
     $type->setMapping($mapping);
     $newDocument = new Document();
     $newDocument->setAutoPopulate();
     $newDocument->set('name', 'Batman');
     $newDocument->set('counter', 1);
     $type->addDocument($newDocument);
     $script = new Script('ctx._source.counter += count; ctx._source.name = name');
     $script->setId($newDocument->getId());
     $script->setParam('count', 2);
     $script->setParam('name', 'robin');
     $script->setUpsert($newDocument);
     try {
         $type->updateDocument($script);
         $this->fail('Update request should fail because source is disabled. Fields param is not set');
     } catch (ResponseException $e) {
         $error = $e->getResponse()->getFullError();
         $this->assertContains('document_source_missing_exception', $error['type']);
     }
     $newDocument->setFieldsSource();
     try {
         $type->updateDocument($newDocument);
         $this->fail('Update request should fail because source is disabled. Fields param is set to _source');
     } catch (ResponseException $e) {
         $error = $e->getResponse()->getFullError();
         $this->assertContains('document_source_missing_exception', $error['type']);
     }
 }