Skip to content

Commit e38cb7f

Browse files
committed
bug #233 [Store] remove minScore parameter from VectorStoreInterface::query (DZunke)
This PR was merged into the main branch. Discussion ---------- [Store] remove `minScore` parameter from `VectorStoreInterface::query` | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Docs? | no | Issues | Fix #195 | License | MIT As discussed within #197 and #195 i have removed the `minScore` argument from the `VectorStoreInterface` as this is semantically not correct for any store. Most stores has never implemented this filter mechanism. It originated from the MongoDB store where it is correct with the internal scoring system but for stores like MariaDB and Postgres the distance calculations are more correct with a `maxScore` option as their results are sorted from `0` as exact match to larger float values for the distance. I have changed those two stores and left the MongoDB store with the minScore implementation. This is a break in the implementation as the interface has changed. Commits ------- 2700074 [Store] remove minScore argument from VectorStoreInterface::query
2 parents 3e8a0b8 + 2700074 commit e38cb7f

File tree

16 files changed

+51
-40
lines changed

16 files changed

+51
-40
lines changed

src/store/doc/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ This leads to a store implementing two methods::
8585
// Implementation to add a document to the store
8686
}
8787

88-
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
88+
public function query(Vector $vector, array $options = []): array
8989
{
9090
// Implementation to query the store for documents
9191
return [];

src/store/src/Bridge/Azure/SearchStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function add(VectorDocument ...$documents): void
4444
]);
4545
}
4646

47-
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
47+
public function query(Vector $vector, array $options = []): array
4848
{
4949
$result = $this->request('search', [
5050
'vectorQueries' => [$this->buildVectorQuery($vector)],

src/store/src/Bridge/ChromaDb/Store.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function add(VectorDocument ...$documents): void
4848
$collection->add($ids, $vectors, $metadata);
4949
}
5050

51-
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
51+
public function query(Vector $vector, array $options = []): array
5252
{
5353
$collection = $this->client->getOrCreateCollection($this->collectionName);
5454
$queryResponse = $collection->query(

src/store/src/Bridge/MariaDb/Store.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,13 @@ public function add(VectorDocument ...$documents): void
100100
/**
101101
* @param array{
102102
* limit?: positive-int,
103+
* maxScore?: float|null,
103104
* } $options
104105
*/
105-
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
106+
public function query(Vector $vector, array $options = []): array
106107
{
108+
$maxScore = $options['maxScore'] ?? null;
109+
107110
$statement = $this->connection->prepare(
108111
\sprintf(
109112
<<<'SQL'
@@ -115,15 +118,15 @@ public function query(Vector $vector, array $options = [], ?float $minScore = nu
115118
SQL,
116119
$this->vectorFieldName,
117120
$this->tableName,
118-
null !== $minScore ? \sprintf('WHERE VEC_DISTANCE_EUCLIDEAN(%1$s, VEC_FromText(:embedding)) >= :minScore', $this->vectorFieldName) : '',
121+
null !== $maxScore ? \sprintf('WHERE VEC_DISTANCE_EUCLIDEAN(%1$s, VEC_FromText(:embedding)) <= :maxScore', $this->vectorFieldName) : '',
119122
$options['limit'] ?? 5,
120123
),
121124
);
122125

123126
$params = ['embedding' => json_encode($vector->getData())];
124127

125-
if (null !== $minScore) {
126-
$params['minScore'] = $minScore;
128+
if (null !== $maxScore) {
129+
$params['maxScore'] = $maxScore;
127130
}
128131

129132
$documents = [];

src/store/src/Bridge/Meilisearch/Store.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function add(VectorDocument ...$documents): void
4848
);
4949
}
5050

51-
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
51+
public function query(Vector $vector, array $options = []): array
5252
{
5353
$result = $this->request('POST', \sprintf('indexes/%s/search', $this->indexName), [
5454
'vector' => $vector->getData(),

src/store/src/Bridge/MongoDb/Store.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,18 @@ public function add(VectorDocument ...$documents): void
103103
* @param array{
104104
* limit?: positive-int,
105105
* numCandidates?: positive-int,
106-
* filter?: array<mixed>
106+
* filter?: array<mixed>,
107+
* minScore?: float,
107108
* } $options
108109
*/
109-
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
110+
public function query(Vector $vector, array $options = []): array
110111
{
112+
$minScore = null;
113+
if (\array_key_exists('minScore', $options)) {
114+
$minScore = $options['minScore'];
115+
unset($options['minScore']);
116+
}
117+
111118
$pipeline = [
112119
[
113120
'$vectorSearch' => array_merge([

src/store/src/Bridge/Neo4j/Store.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function add(VectorDocument ...$documents): void
5555
}
5656
}
5757

58-
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
58+
public function query(Vector $vector, array $options = []): array
5959
{
6060
$response = $this->request('POST', \sprintf('db/%s/query/v2', $this->databaseName), [
6161
'statement' => \sprintf('CALL db.index.vector.queryNodes("%s", 5, $vectors) YIELD node, score RETURN node, score', $this->vectorIndexName),

src/store/src/Bridge/Pinecone/Store.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function add(VectorDocument ...$documents): void
5757
$this->getVectors()->upsert($vectors, $this->namespace);
5858
}
5959

60-
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
60+
public function query(Vector $vector, array $options = []): array
6161
{
6262
$result = $this->getVectors()->query(
6363
vector: $vector->getData(),

src/store/src/Bridge/Postgres/Store.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,13 @@ public function add(VectorDocument ...$documents): void
8787

8888
/**
8989
* @param array<string, mixed> $options
90-
* @param float|null $minScore Minimum score to filter results (optional)
9190
*
9291
* @return VectorDocument[]
9392
*/
94-
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
93+
public function query(Vector $vector, array $options = []): array
9594
{
95+
$maxScore = $options['maxScore'] ?? null;
96+
9697
$sql = \sprintf(<<<SQL
9798
SELECT id, %s AS embedding, metadata, (%s %s :embedding) AS score
9899
FROM %s
@@ -104,16 +105,16 @@ public function query(Vector $vector, array $options = [], ?float $minScore = nu
104105
$this->vectorFieldName,
105106
$this->distance->getComparisonSign(),
106107
$this->tableName,
107-
null !== $minScore ? "WHERE ({$this->vectorFieldName} {$this->distance->getComparisonSign()} :embedding) >= :minScore" : '',
108+
null !== $maxScore ? "WHERE ({$this->vectorFieldName} {$this->distance->getComparisonSign()} :embedding) <= :maxScore" : '',
108109
$options['limit'] ?? 5,
109110
);
110111
$statement = $this->connection->prepare($sql);
111112

112113
$params = [
113114
'embedding' => $this->toPgvector($vector),
114115
];
115-
if (null !== $minScore) {
116-
$params['minScore'] = $minScore;
116+
if (null !== $maxScore) {
117+
$params['maxScore'] = $maxScore;
117118
}
118119

119120
$statement->execute($params);

src/store/src/Bridge/Qdrant/Store.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function add(VectorDocument ...$documents): void
4949
* offset?: positive-int
5050
* } $options
5151
*/
52-
public function query(Vector $vector, array $options = [], ?float $minScore = null): array
52+
public function query(Vector $vector, array $options = []): array
5353
{
5454
$payload = [
5555
'vector' => $vector->getData(),

0 commit comments

Comments
 (0)