-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
120: Indexes::search() improvements r=curquiza a=Guikingone Hi everyone 👋 As mentioned here: #119, here's the `SearchResult` class and the improvements that comes with it. There's just a small "DX" way that I'm not sure about: - When using `SearchResult::filter()` should the `hits` array be reset to 0 or stay as it? IMO, the array could stays as it as we return a new `SearchResult` object but sometimes, it could be good idea to reset the keys from 0 (loops, etc) 🤔 Thanks for the feedback and have a great day 🙂 Co-authored-by: Loulier Guillaume <[email protected]>
- Loading branch information
Showing
6 changed files
with
558 additions
and
11 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
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,198 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MeiliSearch\Search; | ||
|
||
use function array_filter; | ||
use ArrayIterator; | ||
use Countable; | ||
use IteratorAggregate; | ||
|
||
class SearchResult implements Countable, IteratorAggregate | ||
{ | ||
/** | ||
* @var array<int, array<string, mixed>> | ||
*/ | ||
private $hits; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $offset; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $limit; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $nbHits; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $exhaustiveNbHits; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $processingTimeMs; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $query; | ||
|
||
/** | ||
* @var bool|null | ||
*/ | ||
private $exhaustiveFacetsCount; | ||
|
||
/** | ||
* @var array<string, mixed> | ||
*/ | ||
private $facetsDistribution; | ||
|
||
/** | ||
* @var array<string, mixed> | ||
*/ | ||
private $raw; | ||
|
||
public function __construct(array $body) | ||
{ | ||
$this->hits = $body['hits'] ?? []; | ||
$this->offset = $body['offset']; | ||
$this->limit = $body['limit']; | ||
$this->nbHits = $body['nbHits']; | ||
$this->exhaustiveNbHits = $body['exhaustiveNbHits'] ?? false; | ||
$this->processingTimeMs = $body['processingTimeMs']; | ||
$this->query = $body['query']; | ||
$this->exhaustiveFacetsCount = $body['exhaustiveFacetsCount'] ?? null; | ||
$this->facetsDistribution = $body['facetsDistribution'] ?? []; | ||
$this->raw = $body; | ||
} | ||
|
||
/** | ||
* Return a new {@see SearchResult} instance with the hits filtered using `array_filter($this->hits, $callback, ARRAY_FILTER_USE_BOTH)`. | ||
* | ||
* The $callback receives both the current hit and the key, in that order. | ||
* | ||
* The method DOES not trigger a new search. | ||
* | ||
* @return SearchResult | ||
*/ | ||
public function filter(callable $callback): self | ||
{ | ||
$results = array_filter($this->hits, $callback, ARRAY_FILTER_USE_BOTH); | ||
|
||
$this->hits = $results; | ||
$this->nbHits = \count($results); | ||
|
||
return $this; | ||
} | ||
|
||
public function getHit(int $key, $default = null) | ||
{ | ||
return $this->hits[$key] ?? $default; | ||
} | ||
|
||
/** | ||
* @return array<int, array> | ||
*/ | ||
public function getHits(): array | ||
{ | ||
return $this->hits; | ||
} | ||
|
||
public function getOffset(): int | ||
{ | ||
return $this->offset; | ||
} | ||
|
||
public function getLimit(): int | ||
{ | ||
return $this->limit; | ||
} | ||
|
||
public function getHitsCount(): int | ||
{ | ||
return $this->nbHits; | ||
} | ||
|
||
public function getNbHits(): int | ||
{ | ||
return \count($this->hits); | ||
} | ||
|
||
public function getExhaustiveNbHits(): bool | ||
{ | ||
return $this->exhaustiveNbHits; | ||
} | ||
|
||
public function getProcessingTimeMs(): int | ||
{ | ||
return $this->processingTimeMs; | ||
} | ||
|
||
public function getQuery(): string | ||
{ | ||
return $this->query; | ||
} | ||
|
||
public function getExhaustiveFacetsCount(): ?bool | ||
{ | ||
return $this->exhaustiveFacetsCount; | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function getFacetsDistribution(): array | ||
{ | ||
return $this->facetsDistribution; | ||
} | ||
|
||
/** | ||
* Return the original search result. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function getRaw(): array | ||
{ | ||
return $this->raw; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'hits' => $this->hits, | ||
'offset' => $this->offset, | ||
'limit' => $this->limit, | ||
'nbHits' => $this->nbHits, | ||
'hitsCount' => \count($this->hits), | ||
'exhaustiveNbHits' => $this->exhaustiveNbHits, | ||
'processingTimeMs' => $this->processingTimeMs, | ||
'query' => $this->query, | ||
'exhaustiveFacetsCount' => $this->exhaustiveFacetsCount, | ||
'facetsDistribution' => $this->facetsDistribution, | ||
]; | ||
} | ||
|
||
public function toJson(): string | ||
{ | ||
return \json_encode($this->toArray(), JSON_PRETTY_PRINT); | ||
} | ||
|
||
public function getIterator(): ArrayIterator | ||
{ | ||
return new ArrayIterator($this->hits); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return \count($this->hits); | ||
} | ||
} |
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
Oops, something went wrong.