Skip to content

Commit

Permalink
Expose the 'sort' property of results
Browse files Browse the repository at this point in the history
  • Loading branch information
thePanz committed Sep 22, 2021
1 parent 9f71915 commit 6dc8c0b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added `Elastica\Aggregation\NormalizeAggregation` [#1956](https://github.com/ruflin/Elastica/pull/1956)
* 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 `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)
Expand Down
15 changes: 15 additions & 0 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
38 changes: 38 additions & 0 deletions tests/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Elastica\Document;
use Elastica\Mapping;
use Elastica\Query;
use Elastica\Result;
use Elastica\Test\Base as BaseTest;

Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 6dc8c0b

Please sign in to comment.