-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework Sorting to Combinable QueryProperties.
- Found a failure in sorting which also effected rescoring - Add explicit tests for both - Standardized construction for both Rescoring and Sorting
- Loading branch information
1 parent
ffb76aa
commit 93db134
Showing
8 changed files
with
197 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JeroenG\Explorer\Domain\Query\QueryProperties; | ||
|
||
use JeroenG\Explorer\Domain\Syntax\Sort; | ||
|
||
final class Sorting implements QueryProperty, Combinable | ||
{ | ||
/** @param Sort[] $sorts */ | ||
private function __construct(private array $sorts = []) | ||
{ | ||
} | ||
|
||
public static function for(Sort ...$sort): self | ||
{ | ||
return new self($sort); | ||
} | ||
|
||
public function combine(...$self): QueryProperty | ||
{ | ||
$all = $this->sorts; | ||
|
||
/** @var Sorting[] $self */ | ||
foreach($self as $sorting) { | ||
array_push($all, ...$sorting->sorts); | ||
} | ||
|
||
return new self($all); | ||
} | ||
|
||
public function build(): array | ||
{ | ||
return [ | ||
'sort' => array_map( | ||
static fn (Sort $sort) => $sort->build(), | ||
$this->sorts, | ||
), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JeroenG\Explorer\Tests\Unit\Domain\Query\QueryProperties; | ||
|
||
use JeroenG\Explorer\Domain\Query\QueryProperties\Rescorers; | ||
use JeroenG\Explorer\Domain\Query\QueryProperties\Rescoring; | ||
use JeroenG\Explorer\Domain\Syntax\MatchAll; | ||
use JeroenG\Explorer\Domain\Syntax\Term; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class RescorersTest extends TestCase | ||
{ | ||
public function test_it_builds(): void | ||
{ | ||
$sort = Rescorers::for( | ||
Rescoring::create(new Term(':fld:', ':val:')), | ||
); | ||
|
||
self::assertSame(['rescore' => [self::rescoreQueryPart(':fld:', ':val:')]], $sort->build()); | ||
} | ||
|
||
public function test_it_combines(): void | ||
{ | ||
$a = Rescorers::for( | ||
Rescoring::create(new Term(':fld1:', ':val1:')), | ||
Rescoring::create(new Term(':fld2:', ':val2:')), | ||
); | ||
$b = Rescorers::for( | ||
Rescoring::create(new Term(':fld3:', ':val3:')), | ||
Rescoring::create(new Term(':fld4:', ':val4:')), | ||
); | ||
$c = Rescorers::for( | ||
Rescoring::create(new Term(':fld5:', ':val5:')), | ||
); | ||
$d = Rescorers::for(); | ||
|
||
$result = $a->combine($b, $c, $d); | ||
|
||
self::assertNotSame($a->build(), $result->build()); | ||
self::assertSame([ | ||
'rescore' => [ | ||
self::rescoreQueryPart(':fld1:', ':val1:'), | ||
self::rescoreQueryPart(':fld2:', ':val2:'), | ||
self::rescoreQueryPart(':fld3:', ':val3:'), | ||
self::rescoreQueryPart(':fld4:', ':val4:'), | ||
self::rescoreQueryPart(':fld5:', ':val5:'), | ||
], | ||
], $result->build()); | ||
} | ||
|
||
private static function rescoreQueryPart(string $fld, string $val): array | ||
{ | ||
return [ | ||
'window_size' => 10, | ||
'query' => [ | ||
'score_mode' => 'total', | ||
'rescore_query' => [ | ||
'term' => [ | ||
$fld => [ | ||
'value' => $val, | ||
'boost' => 1.0, | ||
] | ||
] | ||
], | ||
'query_weight' => 1.0, | ||
'rescore_query_weight' => 1.0, | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JeroenG\Explorer\Tests\Unit\Domain\Query\QueryProperties; | ||
|
||
use JeroenG\Explorer\Domain\Query\QueryProperties\Sorting; | ||
use JeroenG\Explorer\Domain\Syntax\Sort; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SortingTest extends TestCase | ||
{ | ||
public function test_it_builds_sorting(): void | ||
{ | ||
$sort = Sorting::for( | ||
new Sort(':fld:', Sort::DESCENDING), | ||
); | ||
|
||
self::assertSame([ 'sort' => [ [ ':fld:' => 'desc' ]]],$sort->build()); | ||
} | ||
|
||
public function test_it_combines(): void | ||
{ | ||
$a = Sorting::for( | ||
new Sort(':fld1:', Sort::DESCENDING), | ||
new Sort(':fld2:', Sort::DESCENDING), | ||
); | ||
$b = Sorting::for( | ||
new Sort(':fld3:', Sort::DESCENDING), | ||
new Sort(':fld4:', Sort::DESCENDING), | ||
); | ||
$c = Sorting::for( | ||
new Sort(':fld5:', Sort::DESCENDING), | ||
); | ||
$d = Sorting::for(); | ||
|
||
$result = $a->combine($b, $c, $d); | ||
|
||
self::assertNotSame($a->build(), $result->build()); | ||
self::assertSame([ | ||
'sort' => [ | ||
[ ':fld1:' => 'desc' ], | ||
[ ':fld2:' => 'desc' ], | ||
[ ':fld3:' => 'desc' ], | ||
[ ':fld4:' => 'desc' ], | ||
[ ':fld5:' => 'desc' ], | ||
], | ||
], $result->build()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters