Skip to content

Commit

Permalink
Merge pull request #1033 from intech/patch-1
Browse files Browse the repository at this point in the history
For issue #960
  • Loading branch information
ruflin committed Jan 17, 2016
2 parents f35835e + f2ab7c6 commit 230f00e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file based on the
### Bugfixes

### Added
- `Elastica\Result->getDocument` and `Elastica\ResultSet->getDocuments` for return `\Elastica\Document`. https://github.com/ruflin/Elastica/issues/960

### Improvements
- Add username and password params to connection
Expand Down
18 changes: 18 additions & 0 deletions lib/Elastica/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,24 @@ public function getExplanation()
{
return $this->getParam('_explanation');
}

/**
* Returns Document
*
* @return \Elastica\Document
*/
public function getDocument()
{
$doc = new \Elastica\Document();
$doc->setData($this->getSource());
$hit = $this->getHit();
if($this->hasParam('_source')) unset($hit['_source']);
if($this->hasParam('_explanation')) unset($hit['_explanation']);
if($this->hasParam('highlight')) unset($hit['highlight']);
if($this->hasParam('_score')) unset($hit['_score']);
$doc->setParams($hit);
return $doc;
}

/**
* Magic function to directly access keys inside the result.
Expand Down
14 changes: 14 additions & 0 deletions lib/Elastica/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ public function getResults()
{
return $this->_results;
}

/**
* Returns all Documents.
*
* @return array Documents \Elastica\Document
*/
public function getDocuments()
{
$documents = [];
foreach($this->_results as $doc) {
$documents[] = $doc->getDocument();
}
return $documents;
}

/**
* Returns true if the response contains suggestion results; false otherwise.
Expand Down
30 changes: 30 additions & 0 deletions test/lib/Elastica/Test/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,36 @@ public function testArrayAccess()
$this->assertFalse(isset($resultSet[3]));
}

/**
* @group functional
*/
public function testDocumentsAccess()
{
$index = $this->_createIndex();
$type = $index->getType('test');

$type->addDocuments(array(
new Document(1, array('name' => 'elastica search')),
new Document(2, array('name' => 'elastica library')),
new Document(3, array('name' => 'elastica test')),
));
$index->refresh();

$resultSet = $type->search('elastica search');

$this->assertInstanceOf('Elastica\ResultSet', $resultSet);

$documents = $resultSet->getDocuments();

$this->assertInternalType('array', $documents);
$this->assertEquals(3, count($documents));
$this->assertInstanceOf('Elastica\Document', $documents[0]);
$this->assertInstanceOf('Elastica\Document', $documents[1]);
$this->assertInstanceOf('Elastica\Document', $documents[2]);
$this->assertFalse(isset($documents[3]));
$this->assertEquals('elastica search', $documents[0]->get('name'));
}

/**
* @group functional
* @expectedException \Elastica\Exception\InvalidException
Expand Down
1 change: 1 addition & 0 deletions test/lib/Elastica/Test/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function testGetters()
$result = $resultSet->current();

$this->assertInstanceOf('Elastica\Result', $result);
$this->assertInstanceOf('Elastica\Document', $result->getDocument());
$this->assertEquals($index->getName(), $result->getIndex());
$this->assertEquals($typeName, $result->getType());
$this->assertEquals($docId, $result->getId());
Expand Down

0 comments on commit 230f00e

Please sign in to comment.