Skip to content

Commit ceacf5f

Browse files
Merge #675
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]>
2 parents 97f2fc4 + 7dabe50 commit ceacf5f

File tree

2 files changed

+162
-4
lines changed

2 files changed

+162
-4
lines changed

src/Contracts/SimilarDocumentsQuery.php

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,43 @@ class SimilarDocumentsQuery
1010
* @var int|string
1111
*/
1212
private $id;
13+
14+
/**
15+
* @var non-negative-int|null
16+
*/
1317
private ?int $offset = null;
18+
19+
/**
20+
* @var positive-int|null
21+
*/
1422
private ?int $limit = null;
23+
24+
/**
25+
* @var non-empty-string|null
26+
*/
1527
private ?string $embedder = null;
28+
29+
/**
30+
* @var list<non-empty-string>|null
31+
*/
1632
private ?array $attributesToRetrieve = null;
33+
1734
private ?bool $showRankingScore = null;
35+
1836
private ?bool $showRankingScoreDetails = null;
37+
1938
private ?bool $retrieveVectors = null;
39+
40+
/**
41+
* @var array<int, array<int, string>|string>|null
42+
*/
2043
private ?array $filter = null;
2144

45+
/**
46+
* @var int|float|null
47+
*/
48+
private $rankingScoreThreshold;
49+
2250
/**
2351
* @param int|string $id
2452
*/
@@ -28,7 +56,7 @@ public function __construct($id)
2856
}
2957

3058
/**
31-
* @param non-negative-int $offset
59+
* @param non-negative-int|null $offset
3260
*/
3361
public function setOffset(?int $offset): SimilarDocumentsQuery
3462
{
@@ -38,7 +66,7 @@ public function setOffset(?int $offset): SimilarDocumentsQuery
3866
}
3967

4068
/**
41-
* @param positive-int $limit
69+
* @param positive-int|null $limit
4270
*/
4371
public function setLimit(?int $limit): SimilarDocumentsQuery
4472
{
@@ -108,7 +136,28 @@ public function setRetrieveVectors(?bool $retrieveVectors): SimilarDocumentsQuer
108136
}
109137

110138
/**
111-
* @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
139+
* @param int|float|null $rankingScoreThreshold
140+
*/
141+
public function setRankingScoreThreshold($rankingScoreThreshold): SimilarDocumentsQuery
142+
{
143+
$this->rankingScoreThreshold = $rankingScoreThreshold;
144+
145+
return $this;
146+
}
147+
148+
/**
149+
* @return array{
150+
* id: int|string,
151+
* offset?: non-negative-int,
152+
* limit?: positive-int,
153+
* filter?: array<int, array<int, string>|string>,
154+
* embedder?: non-empty-string,
155+
* attributesToRetrieve?: list<non-empty-string>,
156+
* showRankingScore?: bool,
157+
* showRankingScoreDetails?: bool,
158+
* retrieveVectors?: bool,
159+
* rankingScoreThreshold?: int|float
160+
* }
112161
*/
113162
public function toArray(): array
114163
{
@@ -122,7 +171,8 @@ public function toArray(): array
122171
'showRankingScore' => $this->showRankingScore,
123172
'showRankingScoreDetails' => $this->showRankingScoreDetails,
124173
'retrieveVectors' => $this->retrieveVectors,
125-
], function ($item) {
174+
'rankingScoreThreshold' => $this->rankingScoreThreshold,
175+
], static function ($item) {
126176
return null !== $item;
127177
});
128178
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Contracts;
6+
7+
use Meilisearch\Contracts\SimilarDocumentsQuery;
8+
use PHPUnit\Framework\TestCase;
9+
10+
final class SimilarDocumentsQueryTest extends TestCase
11+
{
12+
/**
13+
* @param int|string $id
14+
*
15+
* @testWith [123]
16+
* ["test"]
17+
*/
18+
public function testConstruct($id): void
19+
{
20+
$data = new SimilarDocumentsQuery($id);
21+
22+
self::assertSame(['id' => $id], $data->toArray());
23+
}
24+
25+
public function testSetOffset(): void
26+
{
27+
$data = (new SimilarDocumentsQuery('test'))->setOffset(66);
28+
29+
self::assertSame(['id' => 'test', 'offset' => 66], $data->toArray());
30+
}
31+
32+
public function testSetLimit(): void
33+
{
34+
$data = (new SimilarDocumentsQuery('test'))->setLimit(50);
35+
36+
self::assertSame(['id' => 'test', 'limit' => 50], $data->toArray());
37+
}
38+
39+
public function testSetFilter(): void
40+
{
41+
$data = (new SimilarDocumentsQuery('test'))->setFilter([
42+
['genres = horror', 'genres = mystery'],
43+
"director = 'Jordan Peele'",
44+
]);
45+
46+
self::assertSame(['id' => 'test', 'filter' => [['genres = horror', 'genres = mystery'], "director = 'Jordan Peele'"]], $data->toArray());
47+
}
48+
49+
public function testSetEmbedder(): void
50+
{
51+
$data = (new SimilarDocumentsQuery('test'))->setEmbedder('default');
52+
53+
self::assertSame(['id' => 'test', 'embedder' => 'default'], $data->toArray());
54+
}
55+
56+
public function testSetAttributesToRetrieve(): void
57+
{
58+
$data = (new SimilarDocumentsQuery('test'))->setAttributesToRetrieve(['name', 'price']);
59+
60+
self::assertSame(['id' => 'test', 'attributesToRetrieve' => ['name', 'price']], $data->toArray());
61+
}
62+
63+
/**
64+
* @testWith [false]
65+
* [true]
66+
*/
67+
public function testSetShowRankingScore(bool $showRankingScore): void
68+
{
69+
$data = (new SimilarDocumentsQuery('test'))->setShowRankingScore($showRankingScore);
70+
71+
self::assertSame(['id' => 'test', 'showRankingScore' => $showRankingScore], $data->toArray());
72+
}
73+
74+
/**
75+
* @testWith [false]
76+
* [true]
77+
*/
78+
public function testSetShowRankingScoreDetails(bool $showRankingScoreDetails): void
79+
{
80+
$data = (new SimilarDocumentsQuery('test'))->setShowRankingScoreDetails($showRankingScoreDetails);
81+
82+
self::assertSame(['id' => 'test', 'showRankingScoreDetails' => $showRankingScoreDetails], $data->toArray());
83+
}
84+
85+
/**
86+
* @testWith [false]
87+
* [true]
88+
*/
89+
public function testSetRetrieveVectors(bool $retrieveVectors): void
90+
{
91+
$data = (new SimilarDocumentsQuery('test'))->setRetrieveVectors($retrieveVectors);
92+
93+
self::assertSame(['id' => 'test', 'retrieveVectors' => $retrieveVectors], $data->toArray());
94+
}
95+
96+
/**
97+
* @testWith [123]
98+
* [0.123]
99+
*
100+
* @param int|float $rankingScoreThreshold
101+
*/
102+
public function testSetRankingScoreThreshold($rankingScoreThreshold): void
103+
{
104+
$data = (new SimilarDocumentsQuery('test'))->setRankingScoreThreshold($rankingScoreThreshold);
105+
106+
self::assertSame(['id' => 'test', 'rankingScoreThreshold' => $rankingScoreThreshold], $data->toArray());
107+
}
108+
}

0 commit comments

Comments
 (0)