Skip to content
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
43 changes: 25 additions & 18 deletions ORM/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace ONGR\ElasticsearchBundle\ORM;

use Elasticsearch\Common\Exceptions\Missing404Exception;
use ONGR\ElasticsearchBundle\Document\DocumentInterface;
use ONGR\ElasticsearchBundle\DSL\Query\TermsQuery;
use ONGR\ElasticsearchBundle\DSL\Search;
Expand Down Expand Up @@ -83,33 +84,38 @@ protected function getTypes()
}

/**
* Returns a single document data by ID.
* Returns a single document data by ID or null if document is not found.
*
* @param string $id
* @param string $id Document Id to find.
*
* @return DocumentInterface|null
*
* @return DocumentInterface
* @throws \LogicException
*/
public function find($id)
{
if (count($this->types) == 1) {
$params = [
'index' => $this->manager->getConnection()->getIndexName(),
'type' => $this->types[0],
'id' => $id,
];
if (count($this->types) !== 1) {
throw new \LogicException('Only one type must be specified for the find() method');
}

$params = [
'index' => $this->manager->getConnection()->getIndexName(),
'type' => $this->types[0],
'id' => $id,
];

try {
$result = $this->manager->getConnection()->getClient()->get($params);
} catch (Missing404Exception $e) {
return null;
}

$converter = new Converter(
$this->manager->getTypesMapping(),
$this->manager->getBundlesMapping()
);
$converter = new Converter(
$this->manager->getTypesMapping(),
$this->manager->getBundlesMapping()
);

return $converter->convertToDocument($result);
} else {
throw new \LogicException('Only one type must be specified for the find() method');
}
return $converter->convertToDocument($result);
}

/**
Expand Down Expand Up @@ -224,9 +230,10 @@ public function suggest($suggesters)
/**
* Removes a single document data by ID.
*
* @param string $id
* @param string $id Document ID to remove.
*
* @return array
*
* @throws \LogicException
*/
public function remove($id)
Expand Down
3 changes: 1 addition & 2 deletions Tests/Functional/Client/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ public function testBulk()
$connection->bulk('delete', 'product', ['_id' => 'baz']);
$connection->commit();

$this->setExpectedException('Elasticsearch\Common\Exceptions\Missing404Exception');
$product = $repository->find('baz');
$this->assertNull($repository->find('baz'), 'Document should not be found.');
}

/**
Expand Down
6 changes: 2 additions & 4 deletions Tests/Functional/ORM/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,12 @@ public function testFind()

/**
* Test repository find on non-existent document.
*
* @expectedException \Elasticsearch\Common\Exceptions\Missing404Exception
*/
public function testFindException()
public function testFindNull()
{
$repo = $this->getManager()->getRepository('AcmeTestBundle:Product');

$repo->find(123);
$this->assertNull($repo->find(123));
}

/**
Expand Down