Skip to content

Commit

Permalink
Remove null from rankingScoreThreshold
Browse files Browse the repository at this point in the history
  • Loading branch information
the-sinner committed Jun 18, 2024
1 parent 490bf71 commit 90545e8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/Contracts/SearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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; });
}
}
40 changes: 35 additions & 5 deletions src/Contracts/SimilarDocumentsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,62 +18,87 @@ 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;

return $this;
}

/**
* @param positive-int $limit
*/
public function setLimit(?int $limit): SimilarDocumentsQuery
{
$this->limit = $limit;

return $this;
}

/**
* @param array<int, array<int, string>|string> $filter an array of arrays representing filter conditions
*/
public function setFilter(array $filter): SimilarDocumentsQuery
{
$this->filter = $filter;

return $this;
}

/**
* @param non-empty-string $embedder
*/
public function setEmbedder(string $embedder): SimilarDocumentsQuery
{
$this->embedder = $embedder;

return $this;
}

/**
* @param list<non-empty-string> $attributesToRetrieve an array of attribute names to retrieve
*/
public function setAttributesToRetrieve(array $attributesToRetrieve): SimilarDocumentsQuery
{
$this->attributesToRetrieve = $attributesToRetrieve;

return $this;
}

/**
* @param bool $showRankingScore boolean value to show ranking score
*/
public function setShowRankingScore(?bool $showRankingScore): SimilarDocumentsQuery
{
$this->showRankingScore = $showRankingScore;

return $this;
}

/**
* @param bool $showRankingScoreDetails boolean value to show ranking score details
*/
public function setShowRankingScoreDetails(?bool $showRankingScoreDetails): SimilarDocumentsQuery
{
$this->showRankingScoreDetails = $showRankingScoreDetails;

return $this;
}

/**
* @return array{id: int|string, offset: non-negative-int, limit: positive-int, filter: array<int, array<int, string>|string>, embedder: non-empty-string, attributesToRetrieve: list<non-empty-string>, showRankingScore: bool, showRankingScoreDetails: bool} SimilarDocumentsQuery converted to an array with non null fields
*/
public function toArray(): array
{
return array_filter([
Expand All @@ -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;
});
}
}
47 changes: 11 additions & 36 deletions src/Search/SimilarDocumentsSearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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<string, mixed>|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<int, array>
* @return array<int, array<string, mixed>>
*/
public function getHits(): array
{
Expand Down Expand Up @@ -103,9 +80,14 @@ public function getHitsCount(): int
return $this->hitsCount;
}

/**
* Converts the SimilarDocumentsSearchResult to an array representation.
*
* @return array<string, mixed>
*/
public function toArray(): array
{
$arr = [
return [
'id' => $this->id,
'hits' => $this->hits,
'hitsCount' => $this->hitsCount,
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions tests/Endpoints/SimilarDocumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 90545e8

Please sign in to comment.