Skip to content

Commit

Permalink
create a federation object for the multi-search
Browse files Browse the repository at this point in the history
  • Loading branch information
irevoire committed Aug 1, 2024
1 parent 8a906a7 commit 47ffe4b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
28 changes: 28 additions & 0 deletions src/Contracts/SearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@

namespace Meilisearch\Contracts;

class MultiSearchFederation
{
private ?int $limit = null;
private ?int $offset = null;

public function setLimit(int $limit): MultiSearchFederation
{
$this->limit = $limit;

return $this;
}

public function setOffset(int $offset): MultiSearchFederation
{
$this->offset = $offset;

return $this;
}

public function toArray(): array
{
return array_filter([
'limit' => $this->limit,
'offset' => $this->offset,
], static function ($item) { return null !== $item; });
}
}

class FederationOptions
{
private ?float $weight = null;
Expand Down
5 changes: 3 additions & 2 deletions src/Endpoints/Delegates/HandlesMultiSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Meilisearch\Endpoints\Delegates;

use Meilisearch\Contracts\Http;
use Meilisearch\Contracts\MultiSearchFederation;

trait HandlesMultiSearch
{
Expand All @@ -13,14 +14,14 @@ trait HandlesMultiSearch
/**
* @param list<\Meilisearch\Contracts\SearchQuery> $queries
*/
public function multiSearch(array $queries = [], array $multiSearchParams = [])
public function multiSearch(array $queries = [], ?MultiSearchFederation $federation = null)
{
$body = [];

foreach ($queries as $query) {
$body[] = $query->toArray();
}

return $this->http->post('/multi-search', ['queries' => $body, ...$multiSearchParams]);
return $this->http->post('/multi-search', ['queries' => $body, 'federation' => $federation->toArray()]);
}
}
3 changes: 2 additions & 1 deletion tests/Endpoints/MultiSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests\Endpoints;

use Meilisearch\Contracts\FederationOptions;
use Meilisearch\Contracts\MultiSearchFederation;
use Meilisearch\Contracts\SearchQuery;
use Meilisearch\Endpoints\Indexes;
use Tests\TestCase;
Expand Down Expand Up @@ -88,7 +89,7 @@ public function testFederation(): void
// By setting the weight to 0.9 this query should appear second
->setFederationOptions((new FederationOptions())->setWeight(0.9)),
],
['federation' => ['limit' => 2]]
(new MultiSearchFederation())->setLimit(2)
);

self::assertArrayHasKey('hits', $response);
Expand Down

0 comments on commit 47ffe4b

Please sign in to comment.