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

Implemented method to remove document #530

Merged
merged 1 commit into from
Jan 5, 2016
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ professional [elasticsearch](https://www.elastic.co/products/elasticsearch) inte

* Supported by [ONGR.io](http://ongr.io) development team.
* Uses the official [elasticsearch-php](https://github.com/elastic/elasticsearch-php) client.
* Ensures full integration with Symfony 2 framework.
* Ensures full integration with Symfony framework.

Technical goodies:

Expand Down
12 changes: 10 additions & 2 deletions Resources/doc/crud.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ $manager->commit();

```php

$repo = $this->get('es.manager.default.content');
$content = $repo->find(5);
$content = $manager->find('AppBundle:Content', 5);
$content->title = 'changed Acme title';
$manager->persist($content);
$manager->commit();
Expand Down Expand Up @@ -134,6 +133,15 @@ $response = $repo->update(1, ['title' => 'new title'], null, ['fields' => 'title

## Delete a document

Document removal can be performed similar to create or update action:

```php
$manager->remove($content);
$manager->commit();
```

Alternatively you can remove document by ID (requires to have repository service):

```php

$repo = $this->get('es.manager.default.content');
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Elasticsearch Bundle

Welcome to the ElasticsearchBundle, the modern solution to work with [Elasticsearch database](https://www.elastic.co/products/elasticsearch) in the [Symfony 2](https://github.com/symfony/symfony-standard) applications. We created this bundle with love :heart: and we think you will love it too.
Welcome to the ElasticsearchBundle, the modern solution to work with [Elasticsearch database](https://www.elastic.co/products/elasticsearch) in the [Symfony](https://github.com/symfony/symfony-standard) applications. We created this bundle with love :heart: and we think you will love it too.

> Bundle set up guide you can find in the `README.md` file.

Expand Down
6 changes: 5 additions & 1 deletion Result/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,17 @@ public function assignArrayToObject(array $array, $object, array $aliases)
*
* @param mixed $object
* @param array $aliases
* @param array $fields
*
* @return array
*/
public function convertToArray($object, $aliases = [])
public function convertToArray($object, $aliases = [], $fields = [])
{
if (empty($aliases)) {
$aliases = $this->getAlias($object);
if (count($fields) > 0) {
$aliases = array_intersect_key($aliases, array_flip($fields));
}
}

$array = [];
Expand Down
20 changes: 20 additions & 0 deletions Service/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,26 @@ public function persist($document)
$this->bulk('index', $type, $documentArray);
}

/**
* Adds document for removal.
*
* @param object $document
*/
public function remove($document)
{
$data = $this->converter->convertToArray($document, [], ['_id']);

if (!isset($data['_id'])) {
throw new \LogicException(
'In order to use remove() method document class must have @MetaField annotation for "_id" field.'
);
}

$type = $this->getMetadataCollector()->getDocumentType(get_class($document));

$this->bulk('delete', $type, ['_id' => $data['_id']]);
}

/**
* Flushes elasticsearch index.
*
Expand Down
29 changes: 29 additions & 0 deletions Tests/Functional/Service/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use ONGR\ElasticsearchBundle\Service\Repository;
use ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\BarBundle\Document\CategoryObject;
use ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\BarBundle\Document\Product;
use ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\BarBundle\Document\WithoutId;
use ONGR\ElasticsearchDSL\Query\TermQuery;
use ONGR\ElasticsearchDSL\Search;
use ONGR\ElasticsearchBundle\Service\Manager;
Expand Down Expand Up @@ -320,4 +321,32 @@ public function testExecuteQueryOnMultipleTypes()

$this->assertCount(5, $result);
}

/**
* Test for remove().
*/
public function testRemove()
{
$manager = $this->getManager('foo');

$product = $manager->find('AcmeBarBundle:Product', 1);
$this->assertInstanceOf('ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\BarBundle\Document\Product', $product);

$manager->remove($product);
$manager->commit();

$product = $manager->find('AcmeBarBundle:Product', 1);
$this->assertNull($product);
}

/**
* Test for remove() in case document has no annotation for ID field.
*
* @expectedException \LogicException
* @expectedExceptionMessage must have @MetaField annotation for "_id"
*/
public function testRemoveException()
{
$this->getManager()->remove(new WithoutId());
}
}
28 changes: 28 additions & 0 deletions Tests/app/fixture/Acme/BarBundle/Document/WithoutId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\BarBundle\Document;

use ONGR\ElasticsearchBundle\Annotation as ES;

/**
* Document without ID meta field.
*
* @ES\Document()
*/
class WithoutId
{
/**
* @var string
* @ES\Property(type="string")
*/
public $description;
}