Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the 'sort' property of results #1979

Merged
merged 1 commit into from
Sep 23, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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