Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toString updated to consider doc_as_upsert if sent an array source #622

Merged
merged 3 commits into from
Jun 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CHANGES

2014-06-01
- toString updated to consider doc_as_upsert if sent an array source #622

2014-05-27
- Fix Aggragations/Filter to work with es v1.2.0 #619

Expand Down
3 changes: 2 additions & 1 deletion lib/Elastica/Bulk/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ public function toString()
if (is_string($source)) {
$string.= $source;
} elseif (is_array($source) && array_key_exists('doc', $source) && is_string($source['doc'])) {
$string.= '{"doc": ' . $source['doc'] . '}';
$docAsUpsert = (isset($source['doc_as_upsert'])) ? ', "doc_as_upsert": '.$source['doc_as_upsert'] : '';
$string.= '{"doc": '.$source['doc'].$docAsUpsert.'}';
} else {
$string.= JSON::stringify($source, 'JSON_ELASTICSEARCH');
}
Expand Down
20 changes: 20 additions & 0 deletions test/lib/Elastica/Test/BulkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,26 @@ public function testUpdate()
$doc = $type->getDocument(6);
$this->assertEquals('test', $doc->test);

//test doc_as_upsert with set of documents (use of addDocuments)
$doc1 = new \Elastica\Document(7, array('test' => 'test1'));
$doc1->setDocAsUpsert(true);
$doc2 = new \Elastica\Document(8, array('test' => 'test2'));
$doc2->setDocAsUpsert(true);
$docs = array($doc1, $doc2);
$bulk = new Bulk($client);
$bulk->setType($type);
$bulk->addDocuments($docs, \Elastica\Bulk\Action::OP_TYPE_UPDATE);
$response = $bulk->send();

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

$index->refresh();
$doc = $type->getDocument(7);
$this->assertEquals('test1', $doc->test);
$doc = $type->getDocument(8);
$this->assertEquals('test2', $doc->test);

//test updating via document with json string as data
$doc3 = $type->createDocument(2);
$bulk = new Bulk($client);
Expand Down