From 90545e8df785a76a5a0424e8c9673763808b4e44 Mon Sep 17 00:00:00 2001 From: Shalabh Agarwal Date: Tue, 18 Jun 2024 19:22:01 +0530 Subject: [PATCH] Remove null from rankingScoreThreshold --- src/Contracts/SearchQuery.php | 2 +- src/Contracts/SimilarDocumentsQuery.php | 40 +++++++++++++++--- src/Search/SimilarDocumentsSearchResult.php | 47 +++++---------------- tests/Endpoints/SimilarDocumentsTest.php | 5 +-- 4 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/Contracts/SearchQuery.php b/src/Contracts/SearchQuery.php index 88c7eeee..2cdb78d7 100644 --- a/src/Contracts/SearchQuery.php +++ b/src/Contracts/SearchQuery.php @@ -238,7 +238,7 @@ public function toArray(): array 'attributesToSearchOn' => $this->attributesToSearchOn, 'showRankingScore' => $this->showRankingScore, 'showRankingScoreDetails' => $this->showRankingScoreDetails, - 'rankingScoreThreshold' => $this->rankingScoreThreshold ?? null, + 'rankingScoreThreshold' => $this->rankingScoreThreshold, ], function ($item) { return null !== $item; }); } } diff --git a/src/Contracts/SimilarDocumentsQuery.php b/src/Contracts/SimilarDocumentsQuery.php index 8c0b3648..cfd041f4 100644 --- a/src/Contracts/SimilarDocumentsQuery.php +++ b/src/Contracts/SimilarDocumentsQuery.php @@ -6,7 +6,10 @@ class SimilarDocumentsQuery { - private int|string $id; + /** + * @var int|string + */ + private $id; private ?int $offset = null; private ?int $limit = null; private ?string $embedder = null; @@ -15,13 +18,17 @@ class SimilarDocumentsQuery private ?bool $showRankingScoreDetails = null; private ?array $filter = null; - public function setId(string|int $id): SimilarDocumentsQuery + /** + * @param int|string $id + */ + public function __construct($id) { $this->id = $id; - - return $this; } + /** + * @param non-negative-int $offset + */ public function setOffset(?int $offset): SimilarDocumentsQuery { $this->offset = $offset; @@ -29,6 +36,9 @@ public function setOffset(?int $offset): SimilarDocumentsQuery return $this; } + /** + * @param positive-int $limit + */ public function setLimit(?int $limit): SimilarDocumentsQuery { $this->limit = $limit; @@ -36,6 +46,9 @@ public function setLimit(?int $limit): SimilarDocumentsQuery return $this; } + /** + * @param array|string> $filter an array of arrays representing filter conditions + */ public function setFilter(array $filter): SimilarDocumentsQuery { $this->filter = $filter; @@ -43,6 +56,9 @@ public function setFilter(array $filter): SimilarDocumentsQuery return $this; } + /** + * @param non-empty-string $embedder + */ public function setEmbedder(string $embedder): SimilarDocumentsQuery { $this->embedder = $embedder; @@ -50,6 +66,9 @@ public function setEmbedder(string $embedder): SimilarDocumentsQuery return $this; } + /** + * @param list $attributesToRetrieve an array of attribute names to retrieve + */ public function setAttributesToRetrieve(array $attributesToRetrieve): SimilarDocumentsQuery { $this->attributesToRetrieve = $attributesToRetrieve; @@ -57,6 +76,9 @@ public function setAttributesToRetrieve(array $attributesToRetrieve): SimilarDoc return $this; } + /** + * @param bool $showRankingScore boolean value to show ranking score + */ public function setShowRankingScore(?bool $showRankingScore): SimilarDocumentsQuery { $this->showRankingScore = $showRankingScore; @@ -64,6 +86,9 @@ public function setShowRankingScore(?bool $showRankingScore): SimilarDocumentsQu return $this; } + /** + * @param bool $showRankingScoreDetails boolean value to show ranking score details + */ public function setShowRankingScoreDetails(?bool $showRankingScoreDetails): SimilarDocumentsQuery { $this->showRankingScoreDetails = $showRankingScoreDetails; @@ -71,6 +96,9 @@ public function setShowRankingScoreDetails(?bool $showRankingScoreDetails): Simi return $this; } + /** + * @return array{id: int|string, offset: non-negative-int, limit: positive-int, filter: array|string>, embedder: non-empty-string, attributesToRetrieve: list, showRankingScore: bool, showRankingScoreDetails: bool} SimilarDocumentsQuery converted to an array with non null fields + */ public function toArray(): array { return array_filter([ @@ -82,6 +110,8 @@ public function toArray(): array 'attributesToRetrieve' => $this->attributesToRetrieve, 'showRankingScore' => $this->showRankingScore, 'showRankingScoreDetails' => $this->showRankingScoreDetails, - ], function ($item) { return null !== $item; }); + ], function ($item) { + return null !== $item; + }); } } diff --git a/src/Search/SimilarDocumentsSearchResult.php b/src/Search/SimilarDocumentsSearchResult.php index 78e6326d..bfcacc44 100644 --- a/src/Search/SimilarDocumentsSearchResult.php +++ b/src/Search/SimilarDocumentsSearchResult.php @@ -21,13 +21,12 @@ class SimilarDocumentsSearchResult implements \Countable, \IteratorAggregate private int $offset; private int $limit; private int $processingTimeMs; - private string $id; public function __construct(array $body) { $this->id = $body['id']; - $this->hits = $body['hits'] ?? []; + $this->hits = $body['hits']; $this->hitsCount = \count($body['hits']); $this->processingTimeMs = $body['processingTimeMs']; $this->offset = $body['offset']; @@ -36,37 +35,15 @@ public function __construct(array $body) } /** - * Return a new {@see SearchResult} instance. - * - * The $options parameter is an array, and the following keys are accepted: - * - transformHits (callable) - * - * The method does NOT trigger a new search. + * @return array|null */ - public function applyOptions($options): self - { - if (\array_key_exists('transformHits', $options) && \is_callable($options['transformHits'])) { - $this->transformHits($options['transformHits']); - } - - return $this; - } - - public function transformHits(callable $callback): self - { - $this->hits = $callback($this->hits); - $this->hitsCount = \count($this->hits); - - return $this; - } - - public function getHit(int $key, $default = null) + public function getHit(int $key): ?array { - return $this->hits[$key] ?? $default; + return $this->hits[$key]; } /** - * @return array + * @return array> */ public function getHits(): array { @@ -103,9 +80,14 @@ public function getHitsCount(): int return $this->hitsCount; } + /** + * Converts the SimilarDocumentsSearchResult to an array representation. + * + * @return array + */ public function toArray(): array { - $arr = [ + return [ 'id' => $this->id, 'hits' => $this->hits, 'hitsCount' => $this->hitsCount, @@ -114,13 +96,6 @@ public function toArray(): array 'limit' => $this->limit, 'estimatedTotalHits' => $this->estimatedTotalHits, ]; - - return $arr; - } - - public function toJSON(): string - { - return json_encode($this->toArray(), JSON_PRETTY_PRINT); } public function getIterator(): \ArrayIterator diff --git a/tests/Endpoints/SimilarDocumentsTest.php b/tests/Endpoints/SimilarDocumentsTest.php index 7648aa32..abddf359 100644 --- a/tests/Endpoints/SimilarDocumentsTest.php +++ b/tests/Endpoints/SimilarDocumentsTest.php @@ -30,10 +30,7 @@ public function testBasicSearchWithSimilarDocuments(): void self::assertSame(1, $response->getHitsCount()); $documentId = $response->getHit(0)['id']; - $response = $this->index->searchSimilarDocuments( - (new SimilarDocumentsQuery()) - ->setId($documentId) - ); + $response = $this->index->searchSimilarDocuments(new SimilarDocumentsQuery($documentId)); self::assertGreaterThanOrEqual(4, $response->getHitsCount()); self::assertArrayHasKey('_vectors', $response->getHit(0));