From 853d4b6133bf13f56b0039076350a85f5b0fef11 Mon Sep 17 00:00:00 2001 From: thePanz <226021+thePanz@users.noreply.github.com> Date: Wed, 22 Sep 2021 11:29:41 +0200 Subject: [PATCH] Expose the 'sort' property of results --- CHANGELOG.md | 1 + src/Result.php | 15 +++++++++++++++ tests/ResultTest.php | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c91b680a7..5f67dc2df3 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added `Elastica\Suggest\Phrase::addDirectGenerator` to align with ES specification [#1964](https://github.com/ruflin/Elastica/pull/1964) * Added support for `psr/log` 2.0 and 3.0 [#1971](https://github.com/ruflin/Elastica/pull/1971) * Added new optional 'case_insensitive' option to `Elastica\Query\Wildcard` [#1894](https://github.com/ruflin/Elastica/pull/1894) +* Added `Elastica\Result::getSort()` fetching the "sort" property of results [#1979](https://github.com/ruflin/Elastica/pull/1979) ### Changed * Updated `php-cs-fixer` to `2.18.6` [#1955](https://github.com/ruflin/Elastica/pull/1955) * Updated `php-cs-fixer` to `3.0.0` [#1959](https://github.com/ruflin/Elastica/pull/1959) diff --git a/src/Result.php b/src/Result.php index b2f4e9c1b6..a89baadf0a 100644 --- a/src/Result.php +++ b/src/Result.php @@ -136,6 +136,21 @@ public function getScore() return $this->getParam('_score'); } + /** + * Returns the sort values of the result. + * Null is returned in case no sorting has been defined for the query. + * + * @return mixed[]|null + */ + public function getSort(): ?array + { + if (!$this->hasParam('sort')) { + return null; + } + + return $this->getParam('sort'); + } + /** * Returns the raw hit array. */ diff --git a/tests/ResultTest.php b/tests/ResultTest.php index 01e428a26d..8167c7edf6 100644 --- a/tests/ResultTest.php +++ b/tests/ResultTest.php @@ -4,6 +4,7 @@ use Elastica\Document; use Elastica\Mapping; +use Elastica\Query; use Elastica\Result; use Elastica\Test\Base as BaseTest; @@ -100,6 +101,43 @@ public function testGetTotalTimeReturnsExpectedResults(): void ); } + /** + * @group functional + */ + public function testGetSort(): void + { + $index = $this->_createIndex(); + $index->addDocument(new Document('3', ['username' => 'hans'])); + $index->refresh(); + + $query = (Query::create(null)->addSort(['_id' => 'desc'])); + $resultSet = $index->search($query); + + $this->assertCount(1, $resultSet->getResults()); + $result = $resultSet->getResults()[0]; + + $this->assertIsArray($result->getSort()); + $this->assertSame(['3'], $result->getSort()); + } + + /** + * @group functional + */ + public function testGetSortWithNoSorting(): void + { + $index = $this->_createIndex(); + $index->addDocument(new Document('3', ['username' => 'hans'])); + $index->refresh(); + + $query = (Query::create(null)); + $resultSet = $index->search($query); + + $this->assertCount(1, $resultSet->getResults()); + $result = $resultSet->getResults()[0]; + + $this->assertNull($result->getSort()); + } + /** * @group unit */