Skip to content

Commit f9d9e4a

Browse files
authored
[BC BREAK] Extend VectorSearchInterface::query() for a $minScore (#151)
1 parent e958270 commit f9d9e4a

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

src/Bridge/Azure/Store/SearchStore.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function add(VectorDocument ...$documents): void
3434
]);
3535
}
3636

37-
public function query(Vector $vector, array $options = []): array
37+
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
3838
{
3939
$result = $this->request('search', [
4040
'vectorQueries' => [$this->buildVectorQuery($vector)],

src/Bridge/ChromaDB/Store.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function add(VectorDocument ...$documents): void
3434
$collection->add($ids, $vectors, $metadata);
3535
}
3636

37-
public function query(Vector $vector, array $options = []): array
37+
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
3838
{
3939
$collection = $this->client->getOrCreateCollection($this->collectionName);
4040
$queryResponse = $collection->query(

src/Bridge/MongoDB/Store.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ public function add(VectorDocument ...$documents): void
9595
* filter?: array<mixed>
9696
* } $options
9797
*/
98-
public function query(Vector $vector, array $options = []): array
98+
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
9999
{
100-
$results = $this->getCollection()->aggregate([
100+
$pipeline = [
101101
[
102102
'$vectorSearch' => array_merge([
103103
'index' => $this->indexName,
@@ -112,7 +112,20 @@ public function query(Vector $vector, array $options = []): array
112112
'score' => ['$meta' => 'vectorSearchScore'],
113113
],
114114
],
115-
], ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]);
115+
];
116+
117+
if (null !== $minScore) {
118+
$pipeline[] = [
119+
'$match' => [
120+
'score' => ['$gte' => $minScore],
121+
],
122+
];
123+
}
124+
125+
$results = $this->getCollection()->aggregate(
126+
$pipeline,
127+
['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]
128+
);
116129

117130
$documents = [];
118131

src/Bridge/Pinecone/Store.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function add(VectorDocument ...$documents): void
4343
$this->getVectors()->upsert($vectors);
4444
}
4545

46-
public function query(Vector $vector, array $options = []): array
46+
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
4747
{
4848
$response = $this->getVectors()->query(
4949
vector: $vector->getData(),

src/Store/VectorStoreInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ interface VectorStoreInterface extends StoreInterface
1414
*
1515
* @return VectorDocument[]
1616
*/
17-
public function query(Vector $vector, array $options = []): array;
17+
public function query(Vector $vector, array $options = [], ?float $minScore = null): array;
1818
}

0 commit comments

Comments
 (0)