Skip to content

Commit

Permalink
Add facet distribution settings to federated search
Browse files Browse the repository at this point in the history
  • Loading branch information
ManyTheFish committed Oct 8, 2024
1 parent 6e038a3 commit 5a4ab70
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
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|null
*/
private ?array $mergeFacets = null;

/**
* @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 $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,
* }
*/
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(['books' => ['genre']])->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

0 comments on commit 5a4ab70

Please sign in to comment.