Skip to content

Commit 230f00e

Browse files
committed
Merge pull request #1033 from intech/patch-1
For issue #960
2 parents f35835e + f2ab7c6 commit 230f00e

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file based on the
1010
### Bugfixes
1111

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

1415
### Improvements
1516
- Add username and password params to connection

lib/Elastica/Result.php

+18
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,24 @@ public function getExplanation()
186186
{
187187
return $this->getParam('_explanation');
188188
}
189+
190+
/**
191+
* Returns Document
192+
*
193+
* @return \Elastica\Document
194+
*/
195+
public function getDocument()
196+
{
197+
$doc = new \Elastica\Document();
198+
$doc->setData($this->getSource());
199+
$hit = $this->getHit();
200+
if($this->hasParam('_source')) unset($hit['_source']);
201+
if($this->hasParam('_explanation')) unset($hit['_explanation']);
202+
if($this->hasParam('highlight')) unset($hit['highlight']);
203+
if($this->hasParam('_score')) unset($hit['_score']);
204+
$doc->setParams($hit);
205+
return $doc;
206+
}
189207

190208
/**
191209
* Magic function to directly access keys inside the result.

lib/Elastica/ResultSet.php

+14
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ public function getResults()
136136
{
137137
return $this->_results;
138138
}
139+
140+
/**
141+
* Returns all Documents.
142+
*
143+
* @return array Documents \Elastica\Document
144+
*/
145+
public function getDocuments()
146+
{
147+
$documents = [];
148+
foreach($this->_results as $doc) {
149+
$documents[] = $doc->getDocument();
150+
}
151+
return $documents;
152+
}
139153

140154
/**
141155
* Returns true if the response contains suggestion results; false otherwise.

test/lib/Elastica/Test/ResultSetTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,36 @@ public function testArrayAccess()
5656
$this->assertFalse(isset($resultSet[3]));
5757
}
5858

59+
/**
60+
* @group functional
61+
*/
62+
public function testDocumentsAccess()
63+
{
64+
$index = $this->_createIndex();
65+
$type = $index->getType('test');
66+
67+
$type->addDocuments(array(
68+
new Document(1, array('name' => 'elastica search')),
69+
new Document(2, array('name' => 'elastica library')),
70+
new Document(3, array('name' => 'elastica test')),
71+
));
72+
$index->refresh();
73+
74+
$resultSet = $type->search('elastica search');
75+
76+
$this->assertInstanceOf('Elastica\ResultSet', $resultSet);
77+
78+
$documents = $resultSet->getDocuments();
79+
80+
$this->assertInternalType('array', $documents);
81+
$this->assertEquals(3, count($documents));
82+
$this->assertInstanceOf('Elastica\Document', $documents[0]);
83+
$this->assertInstanceOf('Elastica\Document', $documents[1]);
84+
$this->assertInstanceOf('Elastica\Document', $documents[2]);
85+
$this->assertFalse(isset($documents[3]));
86+
$this->assertEquals('elastica search', $documents[0]->get('name'));
87+
}
88+
5989
/**
6090
* @group functional
6191
* @expectedException \Elastica\Exception\InvalidException

test/lib/Elastica/Test/ResultTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function testGetters()
3434
$result = $resultSet->current();
3535

3636
$this->assertInstanceOf('Elastica\Result', $result);
37+
$this->assertInstanceOf('Elastica\Document', $result->getDocument());
3738
$this->assertEquals($index->getName(), $result->getIndex());
3839
$this->assertEquals($typeName, $result->getType());
3940
$this->assertEquals($docId, $result->getId());

0 commit comments

Comments
 (0)