本文整理汇总了PHP中Elastica\Script::setUpsert方法的典型用法代码示例。如果您正苦于以下问题:PHP Script::setUpsert方法的具体用法?PHP Script::setUpsert怎么用?PHP Script::setUpsert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Elastica\Script
的用法示例。
在下文中一共展示了Script::setUpsert方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testUpdateDocumentPopulateFields
/**
* @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);
}
示例2: testUpdateDocumentWithoutSource
/**
* @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()->getError();
$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()->getError();
$this->assertContains('document_source_missing_exception', $error['type']);
}
}