Skip to content

Commit

Permalink
Merge #675
Browse files Browse the repository at this point in the history
675: Add `rankingScoreThreshold` to `SimilarDocumentsQuery` r=brunoocasali a=norkunas

# Pull Request

## Related issue
Fixes #671

## What does this PR do?
- ...

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [X] Have you made sure that the title is accurate and descriptive of the changes?


Co-authored-by: Tomas <[email protected]>
  • Loading branch information
meili-bors[bot] and norkunas authored Sep 15, 2024
2 parents 97f2fc4 + 7dabe50 commit ceacf5f
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 4 deletions.
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());
}
}

0 comments on commit ceacf5f

Please sign in to comment.