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

Accept an options array at Type::updateDocument() #686

Merged
merged 12 commits into from
Oct 8, 2014

Conversation

rmruano
Copy link
Contributor

@rmruano rmruano commented Oct 2, 2014

Allows setting options when updating Documents with Scripts:

$myType->updateDocument($myScript, array("retry_on_conflict"=>3, "refresh"=>true);

It's even being used at TypeTest::testUpdateDocument():

public function testUpdateDocument()
{
    ...
    ...
    ...
    $document = new Document();
    $script = new Script(
        "ctx._source.name = name; ctx._source.counter += count",
        array(
            'name' => $newName,
            'count' => 2,
        ),
        null,
        $id
    );
    $script->setUpsert($document);

    $type->updateDocument($script, array('refresh' => true));
    $updatedDoc = $type->getDocument($id)->getData();
    $this->assertEquals($newName, $updatedDoc['name'], "Name was not updated");
    $this->assertEquals(3, $updatedDoc['counter'], "Counter was not incremented");
}

@ruflin
Copy link
Owner

ruflin commented Oct 4, 2014

Makes sense. Can you update the changes.txt file with this change?

Strange that it was already used in the tests. Do have an idea how we could write a specific test for this options?

@rmruano
Copy link
Contributor Author

rmruano commented Oct 5, 2014

Done!

Updating an existing doc with a non-matching version number parameter will rise a VersionConflictEngineException and won't update the document. Perhaps something like this?:

public function testUpdateDocumentWithParameter()
{
    $client = $this->_getClient();
    $index = $client->getIndex('elastica_test');
    $type = $index->getType('update_type');
    $id = 1;
    $type->addDocument(new Document($id, array('name' => 'bruce wayne batman', 'counter' => 1)));
    $newName = 'batman';

    $document = new Document();
    $script = new Script(
        "ctx._source.name = name; ctx._source.counter += count",
        array(
            'name' => $newName,
            'count' => 2,
        ),
        null,
        $id
    );
    $script->setUpsert($document);

    try {
        $type->updateDocument($script, array('version' => 999)); // Wrong version number to make the update fail
    } catch (ResponseException $e) {
        $this->assertContains('VersionConflictEngineException', $e->getMessage());
    }
    $updatedDoc = $type->getDocument($id)->getData();
    $this->assertNotEquals($newName, $updatedDoc['name'], "Name was updated");
    $this->assertNotEquals(3, $updatedDoc['counter'], "Counter was incremented");
}

@ruflin
Copy link
Owner

ruflin commented Oct 5, 2014

Looks good. Can you add this to the pull request?

I made a release yesterday, so make sure to merge in the master branch and please add the pull request id to the changes.txt

@rmruano
Copy link
Contributor Author

rmruano commented Oct 5, 2014

Done, I've added the testUpdateDocumentWithParameter() test, could you take a look?.

@@ -4,6 +4,9 @@ CHANGES
- Release v1.3.4.0
- Update to elasticsearch 1.3.4 #691

2014-10-02
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the date here and move it on top of the release. Otherwise it will be missing in the next release notes.

@ruflin
Copy link
Owner

ruflin commented Oct 5, 2014

Looks good. Almost ready to merge. See my small comment above.

@ruflin ruflin merged commit 9a6f1db into ruflin:master Oct 8, 2014
@ruflin
Copy link
Owner

ruflin commented Oct 8, 2014

Merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants