Skip to content

Commit

Permalink
Add distinct attribute at search (#648)
Browse files Browse the repository at this point in the history
* Add distinct attribute at search

* Update tests/Endpoints/SearchTest.php

---------

Co-authored-by: Many the fish <[email protected]>
  • Loading branch information
the-sinner and ManyTheFish authored Jun 27, 2024
1 parent 707e6e9 commit b7138db
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Contracts/SearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SearchQuery
private ?bool $showRankingScore = null;
private ?bool $showRankingScoreDetails = null;
private ?float $rankingScoreThreshold = null;
private ?string $distinct = null;

public function setQuery(string $q): SearchQuery
{
Expand Down Expand Up @@ -138,6 +139,16 @@ public function setRankingScoreThreshold(?float $rankingScoreThreshold): SearchQ
return $this;
}

/**
* @param non-empty-string|null $distinct
*/
public function setDistinct(?string $distinct): SearchQuery
{
$this->distinct = $distinct;

return $this;
}

public function setSort(array $sort): SearchQuery
{
$this->sort = $sort;
Expand Down Expand Up @@ -239,6 +250,7 @@ public function toArray(): array
'showRankingScore' => $this->showRankingScore,
'showRankingScoreDetails' => $this->showRankingScoreDetails,
'rankingScoreThreshold' => $this->rankingScoreThreshold,
'distinct' => $this->distinct,
], function ($item) { return null !== $item; });
}
}
27 changes: 26 additions & 1 deletion tests/Endpoints/MultiSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected function setUp(): void
parent::setUp();
$this->booksIndex = $this->createEmptyIndex($this->safeIndexName('books'));
$this->booksIndex->updateSortableAttributes(['author']);
$this->booksIndex->updateFilterableAttributes(['genre']);
$promise = $this->booksIndex->updateDocuments(self::DOCUMENTS);
$this->booksIndex->waitForTask($promise['taskUid']);

Expand Down Expand Up @@ -64,7 +65,7 @@ public function testMultiSearch(): void
self::assertArrayHasKey('estimatedTotalHits', $response['results'][0]);
self::assertCount(2, $response['results'][0]['hits']);

self::assertArrayHasKey('indexUid', $response['results'][0]);
self::assertArrayHasKey('indexUid', $response['results'][1]);
self::assertArrayHasKey('hits', $response['results'][1]);
self::assertArrayHasKey('query', $response['results'][1]);
self::assertArrayHasKey('page', $response['results'][1]);
Expand All @@ -90,4 +91,28 @@ public function testSupportedQueryParams(): void
self::assertTrue($result['showRankingScore']);
self::assertTrue($result['showRankingScoreDetails']);
}

public function testMultiSearchWithDistinctAttribute(): void
{
$response = $this->client->multiSearch([
(new SearchQuery())->setIndexUid($this->booksIndex->getUid())
->setFilter(['genre = fantasy']),
(new SearchQuery())->setIndexUid($this->booksIndex->getUid())
->setFilter(['genre = fantasy'])
->setDistinct('genre'),
]);

self::assertCount(2, $response['results']);

self::assertArrayHasKey('hits', $response['results'][0]);
self::assertCount(2, $response['results'][0]['hits']);
self::assertSame('fantasy', $response['results'][0]['hits'][0]['genre']);
self::assertSame('fantasy', $response['results'][0]['hits'][1]['genre']);

self::assertArrayHasKey('indexUid', $response['results'][1]);
self::assertArrayHasKey('hits', $response['results'][1]);
self::assertArrayHasKey('query', $response['results'][1]);
self::assertCount(1, $response['results'][1]['hits']);
self::assertSame('fantasy', $response['results'][1]['hits'][0]['genre']);
}
}
17 changes: 17 additions & 0 deletions tests/Endpoints/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -862,4 +862,21 @@ public function testSearchAndRetrieveFacetStats(): void

self::assertSame(['info.reviewNb' => ['min' => 50.0, 'max' => 1000.0]], $response->getFacetStats());
}

public function testSearchWithDistinctAttribute(): void
{
$this->index = $this->createEmptyIndex($this->safeIndexName());
$this->index->updateFilterableAttributes(['genre']);

$promise = $this->index->updateDocuments(self::DOCUMENTS);
$this->index->waitForTask($promise['taskUid']);

$response = $this->index->search(null, [
'distinct' => 'genre',
'filter' => ['genre = fantasy'],
])->toArray();

self::assertArrayHasKey('title', $response['hits'][0]);
self::assertCount(1, $response['hits']);
}
}

0 comments on commit b7138db

Please sign in to comment.