Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rankingScoreThreshold to SimilarDocumentsQuery #675

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 54 additions & 4 deletions src/Contracts/SimilarDocumentsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,43 @@ class SimilarDocumentsQuery
* @var int|string
*/
private $id;

/**
* @var non-negative-int|null
*/
private ?int $offset = null;

/**
* @var positive-int|null
*/
private ?int $limit = null;

/**
* @var non-empty-string|null
*/
private ?string $embedder = null;

/**
* @var list<non-empty-string>|null
*/
private ?array $attributesToRetrieve = null;

private ?bool $showRankingScore = null;

private ?bool $showRankingScoreDetails = null;

private ?bool $retrieveVectors = null;

/**
* @var array<int, array<int, string>|string>|null
*/
private ?array $filter = null;

/**
* @var int|float|null
*/
private $rankingScoreThreshold;

/**
* @param int|string $id
*/
Expand All @@ -28,7 +56,7 @@ public function __construct($id)
}

/**
* @param non-negative-int $offset
* @param non-negative-int|null $offset
*/
public function setOffset(?int $offset): SimilarDocumentsQuery
{
Expand All @@ -38,7 +66,7 @@ public function setOffset(?int $offset): SimilarDocumentsQuery
}

/**
* @param positive-int $limit
* @param positive-int|null $limit
*/
public function setLimit(?int $limit): SimilarDocumentsQuery
{
Expand Down Expand Up @@ -108,7 +136,28 @@ public function setRetrieveVectors(?bool $retrieveVectors): SimilarDocumentsQuer
}

/**
* @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, retrieveVectors: bool} SimilarDocumentsQuery converted to an array with non null fields
* @param int|float|null $rankingScoreThreshold
*/
public function setRankingScoreThreshold($rankingScoreThreshold): SimilarDocumentsQuery
{
$this->rankingScoreThreshold = $rankingScoreThreshold;

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,
* retrieveVectors?: bool,
* rankingScoreThreshold?: int|float
* }
*/
public function toArray(): array
{
Expand All @@ -122,7 +171,8 @@ public function toArray(): array
'showRankingScore' => $this->showRankingScore,
'showRankingScoreDetails' => $this->showRankingScoreDetails,
'retrieveVectors' => $this->retrieveVectors,
], function ($item) {
'rankingScoreThreshold' => $this->rankingScoreThreshold,
], static function ($item) {
return null !== $item;
});
}
Expand Down
108 changes: 108 additions & 0 deletions tests/Contracts/SimilarDocumentsQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Contracts;

use Meilisearch\Contracts\SimilarDocumentsQuery;
use PHPUnit\Framework\TestCase;

final class SimilarDocumentsQueryTest extends TestCase
{
/**
* @param int|string $id
*
* @testWith [123]
* ["test"]
*/
public function testConstruct($id): void
{
$data = new SimilarDocumentsQuery($id);

self::assertSame(['id' => $id], $data->toArray());
}

public function testSetOffset(): void
{
$data = (new SimilarDocumentsQuery('test'))->setOffset(66);

self::assertSame(['id' => 'test', 'offset' => 66], $data->toArray());
}

public function testSetLimit(): void
{
$data = (new SimilarDocumentsQuery('test'))->setLimit(50);

self::assertSame(['id' => 'test', 'limit' => 50], $data->toArray());
}

public function testSetFilter(): void
{
$data = (new SimilarDocumentsQuery('test'))->setFilter([
['genres = horror', 'genres = mystery'],
"director = 'Jordan Peele'",
]);

self::assertSame(['id' => 'test', 'filter' => [['genres = horror', 'genres = mystery'], "director = 'Jordan Peele'"]], $data->toArray());
}

public function testSetEmbedder(): void
{
$data = (new SimilarDocumentsQuery('test'))->setEmbedder('default');

self::assertSame(['id' => 'test', 'embedder' => 'default'], $data->toArray());
}

public function testSetAttributesToRetrieve(): void
{
$data = (new SimilarDocumentsQuery('test'))->setAttributesToRetrieve(['name', 'price']);

self::assertSame(['id' => 'test', 'attributesToRetrieve' => ['name', 'price']], $data->toArray());
}

/**
* @testWith [false]
* [true]
*/
public function testSetShowRankingScore(bool $showRankingScore): void
{
$data = (new SimilarDocumentsQuery('test'))->setShowRankingScore($showRankingScore);

self::assertSame(['id' => 'test', 'showRankingScore' => $showRankingScore], $data->toArray());
}

/**
* @testWith [false]
* [true]
*/
public function testSetShowRankingScoreDetails(bool $showRankingScoreDetails): void
{
$data = (new SimilarDocumentsQuery('test'))->setShowRankingScoreDetails($showRankingScoreDetails);

self::assertSame(['id' => 'test', 'showRankingScoreDetails' => $showRankingScoreDetails], $data->toArray());
}

/**
* @testWith [false]
* [true]
*/
public function testSetRetrieveVectors(bool $retrieveVectors): void
{
$data = (new SimilarDocumentsQuery('test'))->setRetrieveVectors($retrieveVectors);

self::assertSame(['id' => 'test', 'retrieveVectors' => $retrieveVectors], $data->toArray());
}

/**
* @testWith [123]
* [0.123]
*
* @param int|float $rankingScoreThreshold
*/
public function testSetRankingScoreThreshold($rankingScoreThreshold): void
{
$data = (new SimilarDocumentsQuery('test'))->setRankingScoreThreshold($rankingScoreThreshold);

self::assertSame(['id' => 'test', 'rankingScoreThreshold' => $rankingScoreThreshold], $data->toArray());
}
}
Loading