-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef948fe
commit eb909ac
Showing
4 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Meilisearch\Contracts; | ||
|
||
class FacetSearchQuery | ||
{ | ||
private ?string $q = null; | ||
private ?string $matchingStrategy = null; | ||
private ?array $filter = null; | ||
private ?string $facetQuery = null; | ||
private ?string $facetName = null; | ||
|
||
public function setQuery(string $q): FacetSearchQuery | ||
{ | ||
$this->q = $q; | ||
|
||
return $this; | ||
} | ||
|
||
public function setMatchingStrategy(string $matchingStrategy): FacetSearchQuery | ||
{ | ||
$this->matchingStrategy = $matchingStrategy; | ||
|
||
return $this; | ||
} | ||
|
||
public function setFilter(string $filter): FacetSearchQuery | ||
{ | ||
$this->filter = $filter; | ||
|
||
return $this; | ||
} | ||
|
||
public function setFacetQuery(string $facetQuery): FacetSearchQuery | ||
{ | ||
$this->facetQuery = $facetQuery; | ||
|
||
return $this; | ||
} | ||
|
||
public function setFacetName(string $facetName): FacetSearchQuery | ||
{ | ||
$this->facetName = $facetName; | ||
|
||
return $this; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return array_filter([ | ||
'q' => $this->q, | ||
'matchingStrategy' => $this->matchingStrategy, | ||
'filter' => $this->filter, | ||
'facetQuery' => $this->facetQuery, | ||
'facetName' => $this->facetName, | ||
], function ($item) { return null !== $item; }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Meilisearch\Search; | ||
|
||
class FacetSearchResult implements \Countable, \IteratorAggregate | ||
{ | ||
/** | ||
* @var array<int, array<string, mixed>> | ||
*/ | ||
private array $facetHits; | ||
private int $processingTimeMs; | ||
private ?string $facetQuery; | ||
|
||
public function __construct(array $body) | ||
{ | ||
$this->facetHits = $body['facetHits'] ?? []; | ||
$this->facetQuery = $body['facetQuery']; | ||
$this->processingTimeMs = $body['processingTimeMs']; | ||
} | ||
|
||
/** | ||
* @return array<int, array> | ||
*/ | ||
public function getFacetHits(): array | ||
{ | ||
return $this->facetHits; | ||
} | ||
|
||
public function getProcessingTimeMs(): int | ||
{ | ||
return $this->processingTimeMs; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'facetHits' => $this->facetHits, | ||
'facetQuery' => $this->facetQuery, | ||
'processingTimeMs' => $this->processingTimeMs, | ||
]; | ||
} | ||
|
||
public function toJSON(): string | ||
{ | ||
return json_encode($this->toArray(), JSON_PRETTY_PRINT); | ||
} | ||
|
||
public function getIterator(): \ArrayIterator | ||
{ | ||
return new \ArrayIterator($this->facetHits); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return \count($this->facetHits); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Endpoints; | ||
|
||
use Meilisearch\Contracts\FacetSearchQuery; | ||
use Meilisearch\Endpoints\Indexes; | ||
use Tests\TestCase; | ||
|
||
final class FacetSearchTest extends TestCase | ||
{ | ||
private Indexes $index; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->index = $this->createEmptyIndex($this->safeIndexName()); | ||
$this->index->updateDocuments(self::DOCUMENTS); | ||
$promise = $this->index->updateFilterableAttributes(['genre']); | ||
$this->index->waitForTask($promise['taskUid']); | ||
} | ||
|
||
public function testBasicSearchWithFilters(): void | ||
{ | ||
$response = $this->index->search('prince', ['facets' => ['genre']]); | ||
|
||
$this->assertSame(array_keys($response->getFacetDistribution()['genre']), [ | ||
'adventure', 'fantasy', | ||
]); | ||
|
||
$response = $this->index->facetSearch( | ||
(new FacetSearchQuery()) | ||
->setFacetQuery('fa') | ||
->setFacetName('genre') | ||
->setQuery('prince') | ||
); | ||
|
||
$this->assertSame(array_column($response->getFacetHits(), 'value'), ['fantasy']); | ||
} | ||
} |