-
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.
Merge pull request #207 from Jeroen-G/rework-rescore-to-query-properties
Rework rescoring to be query property
- Loading branch information
Showing
11 changed files
with
288 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JeroenG\Explorer\Domain\Query\QueryProperties; | ||
|
||
interface Combinable | ||
{ | ||
/** | ||
* @param static ...$self | ||
*/ | ||
public function combine(...$self): QueryProperty; | ||
} |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JeroenG\Explorer\Domain\Query\QueryProperties; | ||
|
||
final class Rescorers implements QueryProperty, Combinable | ||
{ | ||
private function __construct( | ||
private array $rescoringQueries = [], | ||
) {} | ||
|
||
public static function for(Rescoring ...$rescoring): self | ||
{ | ||
return new self($rescoring); | ||
} | ||
|
||
public function build(): array | ||
{ | ||
return [ | ||
'rescore' => array_map(static fn (Rescoring $rescoring) => $rescoring->build(), $this->rescoringQueries), | ||
]; | ||
} | ||
|
||
public function combine(...$self): self | ||
{ | ||
$all = $this->rescoringQueries; | ||
foreach($self as $rescorer) { | ||
array_push($all, ...$rescorer->rescoringQueries); | ||
} | ||
|
||
return new self($all); | ||
} | ||
} |
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()); | ||
} | ||
} |
Oops, something went wrong.