Skip to content

Commit

Permalink
Merge pull request #10 from spiritinlife/feature/add-rescore
Browse files Browse the repository at this point in the history
Add method to set rescore to search request
  • Loading branch information
babenkoivan authored May 17, 2021
2 parents 5ccbbc3 + 171afbb commit d3b5daa
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,23 @@ $request->setSort([
'_score'
]);

// rescore documents
$request->setRescore([
'window_size' => 50,
'query' => [
'rescore_query' => [
'match_phrase' => [
'message' => [
'query' => 'the quick brown',
'slop' => 2,
],
],
],
'query_weight' => 0.7,
'rescore_query_weight' => 1.2,
]
]);

// add a post filter
$request->setPostFilter([
'term' => [
Expand Down
11 changes: 11 additions & 0 deletions src/Search/SearchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ final class SearchRequest implements ArrayableInterface
* @var array|null
*/
private $sort;
/**
* @var array|null
*/
private $rescore;
/**
* @var int|null
*/
Expand Down Expand Up @@ -80,6 +84,12 @@ public function setSort(array $sort): self
return $this;
}

public function setRescore(array $rescore): self
{
$this->rescore = $rescore;
return $this;
}

public function setFrom(int $from): self
{
$this->from = $from;
Expand Down Expand Up @@ -161,6 +171,7 @@ public function toArray(): array
foreach ([
'highlight' => 'highlight',
'sort' => 'sort',
'rescore' => 'rescore',
'from' => 'from',
'size' => 'size',
'suggest' => 'suggest',
Expand Down
44 changes: 44 additions & 0 deletions tests/Unit/Search/SearchRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,50 @@ public function test_array_casting_with_query_and_sort(): void
], $request->toArray());
}

public function test_array_casting_with_query_and_rescore(): void
{
$request = new SearchRequest([
'match_all' => new stdClass(),
]);

$request->setRescore([
'window_size' => 50,
'query' => [
'rescore_query' => [
'match_phrase' => [
'message' => [
'query' => 'the quick brown',
'slop' => 2,
],
],
],
'query_weight' => 0.7,
'rescore_query_weight' => 1.2,
],
]);

$this->assertEquals([
'query' => [
'match_all' => new stdClass(),
],
'rescore' => [
'window_size' => 50,
'query' => [
'rescore_query' => [
'match_phrase' => [
'message' => [
'query' => 'the quick brown',
'slop' => 2,
],
],
],
'query_weight' => 0.7,
'rescore_query_weight' => 1.2,
],
],
], $request->toArray());
}

public function test_array_casting_with_query_and_from(): void
{
$request = new SearchRequest([
Expand Down

0 comments on commit d3b5daa

Please sign in to comment.