Skip to content

Commit

Permalink
Ensure true or false flag (#1442)
Browse files Browse the repository at this point in the history
Resolve #1435
  • Loading branch information
silvioq authored and ruflin committed Jan 31, 2018
1 parent 6ef779d commit c427a29
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file based on the
### Bugfixes
- Characters "<" and ">" will be removed when a query term is passed to [`Util::escapeTerm`](https://github.com/ruflin/Elastica/pull/1415/files). Since v5.1 the [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-query-string-query.html#_reserved_characters) states that these symbols cannot be escaped ever.
- Remove [`each()`](http://www.php.net/each) usage to fix PHP 7.2 compatibility
- Fix [#1435](https://github.com/ruflin/Elastica/issues/1435) forcing `doc_as_upsert` to be boolean, acording [Elastic doc-update documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_literal_doc_as_upsert_literal)

### Added

Expand Down
6 changes: 5 additions & 1 deletion lib/Elastica/Bulk/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ public function toString()
if (is_string($source)) {
$string .= $source;
} elseif (is_array($source) && array_key_exists('doc', $source) && is_string($source['doc'])) {
$docAsUpsert = (isset($source['doc_as_upsert'])) ? ', "doc_as_upsert": '.$source['doc_as_upsert'] : '';
if (isset($source['doc_as_upsert'])) {
$docAsUpsert = ', "doc_as_upsert": ' . ($source['doc_as_upsert'] ? 'true' : 'false' );
} else {
$docAsUpsert = '';
}
$string .= '{"doc": '.$source['doc'].$docAsUpsert.'}';
} else {
$string .= JSON::stringify($source, JSON_UNESCAPED_UNICODE);
Expand Down
97 changes: 97 additions & 0 deletions test/Elastica/Bulk/Action/UpdateDocumentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
namespace Elastica\Test\BulkAction;

use Elastica\Bulk\Action\UpdateDocument;
use Elastica\Index;
use Elastica\Test\Base as BaseTest;
use Elastica\Type;
use Elastica\Document;

class ActionTest extends BaseTest
{
/**
* @group unit
*/
public function testUpdateDocument()
{
$document = new Document('', ['foo' => 'bar']);
$action = new UpdateDocument($document);
$this->assertEquals('update', $action->getOpType());
$this->assertTrue($action->hasSource());

$docExpected = '{"doc":{"foo":"bar"}}'."\n";
$expected = '{"update":{}}'."\n";
$expected .= $docExpected;
$this->assertEquals($expected, $action->toString());

$action->setIndex('index');

$expected = '{"update":{"_index":"index"}}'."\n";
$expected .= $docExpected;
$this->assertEquals($expected, $action->toString());

$action->setType('type');

$expected = '{"update":{"_index":"index","_type":"type"}}'."\n";
$expected .= $docExpected;
$this->assertEquals($expected, $action->toString());

$action->setId(1);
$expected = '{"update":{"_index":"index","_type":"type","_id":1}}'."\n";
$expected .= $docExpected;
$this->assertEquals($expected, $action->toString());

$action->setRouting(1);
$expected = '{"update":{"_index":"index","_type":"type","_id":1,"_routing":1}}'."\n";
$expected .= $docExpected;
$this->assertEquals($expected, $action->toString());

$client = $this->_getClient();
$index = new Index($client, 'index2');
$type = new Type($index, 'type2');

$action->setIndex($index);

$expected = '{"update":{"_index":"index2","_type":"type","_id":1,"_routing":1}}'."\n";
$expected .= $docExpected;
$this->assertEquals($expected, $action->toString());

$action->setType($type);

$expected = '{"update":{"_index":"index2","_type":"type2","_id":1,"_routing":1}}'."\n";
$expected .= $docExpected;
$this->assertEquals($expected, $action->toString());

}

/**
* @group unit
*/
public function testUpdateDocumentAsUpsert()
{
$document = new Document(1, ['foo' => 'bar'], 'type', 'index');
$document->setDocAsUpsert(true);
$action = new UpdateDocument($document);

$this->assertEquals('update', $action->getOpType());
$this->assertTrue($action->hasSource());

$expected = '{"update":{"_index":"index","_type":"type","_id":1}}'."\n"
. '{"doc":{"foo":"bar"},"doc_as_upsert":true}'."\n";
$this->assertEquals($expected, $action->toString());

$document->setDocAsUpsert(1);
$action->setDocument($document);
$this->assertEquals($expected, $action->toString());

$document->setDocAsUpsert(false);
$action->setDocument($document);
$expected = '{"update":{"_index":"index","_type":"type","_id":1}}'."\n"
. '{"doc":{"foo":"bar"}}'."\n";
$this->assertEquals($expected, $action->toString());

$document->setDocAsUpsert(0);
$action->setDocument($document);
$this->assertEquals($expected, $action->toString());
}
}
45 changes: 45 additions & 0 deletions test/Elastica/BulkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,51 @@ public function testUpdate()
$index->delete();
}

/**
* @group functional
*/
public function testUpsert()
{
$index = $this->_createIndex();
$type = $index->getType('bulk_test');
$client = $index->getClient();

$doc1 = $type->createDocument(1, ['name' => 'Pele']);
$doc2 = $type->createDocument(2, ['name' => 'Beckenbauer']);
$doc3 = $type->createDocument(3, ['name' => 'Baggio']);
$doc4 = $type->createDocument(4, ['name' => 'Cruyff']);
$documents = array_map(function($d){ $d->setDocAsUpsert(true); return $d;}, [$doc1, $doc2, $doc3, $doc4]);

//index some documents
$bulk = new Bulk($client);
$bulk->setType($type);
$bulk->addDocuments($documents);
$response = $bulk->send();

$this->assertTrue($response->isOk());
$this->assertFalse($response->hasError());

$index->refresh();

//test updating via document
$doc1 = $type->createDocument(1, ['name' => 'Maradona']);
$doc1->setDocAsUpsert(true);
$bulk = new Bulk($client);
$bulk->setType($type);
$updateAction = new UpdateDocument($doc1);
$bulk->addAction($updateAction);
$response = $bulk->send();

$this->assertTrue($response->isOk());
$this->assertFalse($response->hasError());

$index->refresh();

$doc = $type->getDocument(1);
$docData = $doc->getData();
$this->assertEquals('Maradona', $docData['name']);
}

/**
* @group unit
*/
Expand Down

0 comments on commit c427a29

Please sign in to comment.