Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi search facet distribution #688

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/Contracts/MultiSearchFederation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ class MultiSearchFederation
*/
private ?int $offset = null;

/**
* @var array<non-empty-string, list<non-empty-string>>|null
*/
private ?array $facetsByIndex = null;

/**
* @var array{maxValuesPerFacet: positive-int}|null
*/
private ?array $mergeFacets = null;
norkunas marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param non-negative-int $limit
*
Expand All @@ -40,17 +50,45 @@ public function setOffset(int $offset): self
return $this;
}

/**
* @param array<non-empty-string, list<non-empty-string>> $facetsByIndex
*
* @return $this
*/
public function setFacetsByIndex(array $facetsByIndex): self
{
$this->facetsByIndex = $facetsByIndex;

return $this;
}

/**
* @param array{maxValuesPerFacet: positive-int} $mergeFacets
*
* @return $this
*/
public function setMergeFacets(array $mergeFacets): self
{
$this->mergeFacets = $mergeFacets;

return $this;
}

/**
* @return array{
* limit?: non-negative-int,
* offset?: non-negative-int
* offset?: non-negative-int,
* facetsByIndex?: array<non-empty-string, list<non-empty-string>>,
* mergeFacets?: array{maxValuesPerFacet: positive-int},
* }
*/
public function toArray(): array
{
return array_filter([
'limit' => $this->limit,
'offset' => $this->offset,
'facetsByIndex' => $this->facetsByIndex,
'mergeFacets' => $this->mergeFacets,
], static function ($item) { return null !== $item; });
}
}
14 changes: 14 additions & 0 deletions tests/Contracts/MultiSearchFederationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,18 @@ public function testSetOffset(): void

self::assertSame(['offset' => 5], $data->toArray());
}

public function testSetFacetsByIndex(): void
{
$data = (new MultiSearchFederation())->setFacetsByIndex(['books' => ['author', 'genre']]);

self::assertSame(['facetsByIndex' => ['books' => ['author', 'genre']]], $data->toArray());
}

public function testSetMergeFacets(): void
{
$data = (new MultiSearchFederation())->setMergeFacets(['maxValuesPerFacet' => 10]);

self::assertSame(['mergeFacets' => ['maxValuesPerFacet' => 10]], $data->toArray());
}
}
3 changes: 2 additions & 1 deletion tests/Endpoints/MultiSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,15 @@ public function testFederation(): void
// By setting the weight to 0.9 this query should appear second
->setFederationOptions((new FederationOptions())->setWeight(0.9)),
],
(new MultiSearchFederation())->setLimit(2)
(new MultiSearchFederation())->setLimit(2)->setFacetsByIndex([$this->booksIndex->getUid() => ['genre'], $this->songsIndex->getUid() => ['duration-float']])->setMergeFacets(['maxValuesPerFacet' => 10])
);

self::assertArrayHasKey('hits', $response);
self::assertArrayHasKey('processingTimeMs', $response);
self::assertArrayHasKey('limit', $response);
self::assertArrayHasKey('offset', $response);
self::assertArrayHasKey('estimatedTotalHits', $response);
self::assertArrayHasKey('facetDistribution', $response);
self::assertCount(2, $response['hits']);
self::assertSame(2, $response['limit']);
self::assertSame(0, $response['offset']);
Expand Down