Skip to content

Commit

Permalink
EZP-32107: Added Score sort clause (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed Nov 4, 2020
1 parent e7363ba commit 500e270
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ services:
date_published: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\DatePublished
section_identifier: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\SectionIdentifier
section_name: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\SectionName
score: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\Score
location_depth: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\Location\Depth
location_id: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\Location\Id
location_is_main: \eZ\Publish\API\Repository\Values\Content\Query\SortClause\Location\IsMainLocation
Expand Down
177 changes: 177 additions & 0 deletions eZ/Publish/API/Repository/Tests/SearchService/SortClause/ScoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\API\Repository\Tests\SearchService\SortClause;

use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Tests\BaseTest;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;

final class ScoreTest extends BaseTest
{
protected function setUp(): void
{
parent::setUp();

$searchService = $this->getRepository()->getSearchService();
if (!$searchService->supports(SearchService::CAPABILITY_SCORING)) {
self::markTestSkipped("Search engine doesn't support scoring");
}
}

/**
* @param string[] $values
*
* @throws \eZ\Publish\API\Repository\Exceptions\Exception
*
* @dataProvider dataProviderForTestSortingByScore
*/
public function testSortingByScore(iterable $inputValues, Query $query, array $expectedOrderedIds): void
{
$this->createContentForScoreSortTesting($inputValues);

$searchService = $this->getRepository()->getSearchService();
if ($query instanceof LocationQuery) {
$actualResults = $searchService->findLocations($query);
} else {
$actualResults = $searchService->findContentInfo($query);
}

$this->assertSearchResultOrderByRemoteId($expectedOrderedIds, $actualResults);
}

public function dataProviderForTestSortingByScore(): iterable
{
// The following input values for test content guarantee predictable scoring
$inputValues = ['foo foo', 'foo', 'foo foo foo'];

yield 'content asc' => [
$inputValues,
new Query([
'query' => new Criterion\FullText('foo'),
'sortClauses' => [
new SortClause\Score(Query::SORT_ASC),
new SortClause\ContentId(),
],
]),
['foo', 'foo foo', 'foo foo foo'],
];

yield 'content desc' => [
$inputValues,
new Query([
'query' => new Criterion\FullText('foo'),
'sortClauses' => [
new SortClause\Score(Query::SORT_DESC),
new SortClause\ContentId(),
],
]),
['foo foo foo', 'foo foo', 'foo'],
];

yield 'location asc' => [
$inputValues,
new LocationQuery([
'query' => new Criterion\FullText('foo'),
'sortClauses' => [
new SortClause\Score(Query::SORT_ASC),
new SortClause\ContentId(),
],
]),
['foo', 'foo foo', 'foo foo foo'],
];

yield 'location desc' => [
$inputValues,
new LocationQuery([
'query' => new Criterion\FullText('foo'),
'sortClauses' => [
new SortClause\Score(Query::SORT_DESC),
new SortClause\ContentId(),
],
]),
['foo foo foo', 'foo foo', 'foo'],
];
}

private function assertSearchResultOrderByRemoteId(
array $expectedOrderedIds,
SearchResult $actualSearchResults
): void {
self::assertEquals(
count($expectedOrderedIds),
$actualSearchResults->totalCount
);

$actualIds = array_map(
static function (SearchHit $searchHit): string {
return $searchHit->valueObject->remoteId;
},
$actualSearchResults->searchHits
);

self::assertEquals($expectedOrderedIds, $actualIds);
}

/**
* @param string[] $values
*
* @throws \eZ\Publish\API\Repository\Exceptions\Exception
*/
private function createContentForScoreSortTesting(iterable $values): void
{
$repository = $this->getRepository();
$contentService = $repository->getContentService();
$locationService = $repository->getLocationService();
$contentTypeService = $repository->getContentTypeService();

$contentTypeCreateStruct = $contentTypeService->newContentTypeCreateStruct('score_sort_test');
$contentTypeCreateStruct->mainLanguageCode = 'eng-GB';
$contentTypeCreateStruct->names = ['eng-GB' => 'score_sort_test'];
$contentTypeCreateStruct->creatorId = 14;
$contentTypeCreateStruct->creationDate = new \DateTime();

$fieldCreate = $contentTypeService->newFieldDefinitionCreateStruct('value', 'ezstring');
$fieldCreate->names = ['eng-GB' => 'value'];
$fieldCreate->fieldGroup = 'main';
$fieldCreate->position = 1;

$contentTypeCreateStruct->addFieldDefinition($fieldCreate);

$contentGroup = $contentTypeService->loadContentTypeGroupByIdentifier('Content');
$contentTypeDraft = $contentTypeService->createContentType($contentTypeCreateStruct, [$contentGroup]);
$contentTypeService->publishContentTypeDraft($contentTypeDraft);
$contentType = $contentTypeService->loadContentType($contentTypeDraft->id);

foreach ($values as $value) {
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, 'eng-GB');
$contentCreateStruct->remoteId = $value;
$contentCreateStruct->alwaysAvailable = false;
$contentCreateStruct->setField('value', $value);

$locationCreateStruct = $locationService->newLocationCreateStruct(2);
$locationCreateStruct->remoteId = $value;

$draft = $contentService->createContent(
$contentCreateStruct,
[
$locationCreateStruct,
]
);

$contentService->publishVersion($draft->getVersionInfo());
}

$this->refreshSearch($repository);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\API\Repository\Values\Content\Query\SortClause;

use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;

/**
* Sorts search results by relevance score.
*/
class Score extends SortClause
{
public function __construct(string $sortDirection = Query::SORT_ASC)
{
parent::__construct('score', $sortDirection);
}
}

0 comments on commit 500e270

Please sign in to comment.