Skip to content

Commit 3bdcb79

Browse files
committed
[Store][Pg] Add support for custom "where" expression
1 parent f40fcda commit 3bdcb79

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,20 @@ public function add(VectorDocument ...$documents): void
9292
*/
9393
public function query(Vector $vector, array $options = []): array
9494
{
95+
$where = null;
96+
9597
$maxScore = $options['maxScore'] ?? null;
98+
if ($maxScore) {
99+
$where = "WHERE ({$this->vectorFieldName} {$this->distance->getComparisonSign()} :embedding) <= :maxScore";
100+
}
101+
102+
if ($options['where'] ?? false) {
103+
if ($where) {
104+
$where .= ' AND ('.$options['where'].')';
105+
} else {
106+
$where = 'WHERE '.$options['where'];
107+
}
108+
}
96109

97110
$sql = \sprintf(<<<SQL
98111
SELECT id, %s AS embedding, metadata, (%s %s :embedding) AS score
@@ -105,13 +118,14 @@ public function query(Vector $vector, array $options = []): array
105118
$this->vectorFieldName,
106119
$this->distance->getComparisonSign(),
107120
$this->tableName,
108-
null !== $maxScore ? "WHERE ({$this->vectorFieldName} {$this->distance->getComparisonSign()} :embedding) <= :maxScore" : '',
121+
$where ?? '',
109122
$options['limit'] ?? 5,
110123
);
111124
$statement = $this->connection->prepare($sql);
112125

113126
$params = [
114127
'embedding' => $this->toPgvector($vector),
128+
...$options['params'] ?? [],
115129
];
116130
if (null !== $maxScore) {
117131
$params['maxScore'] = $maxScore;

0 commit comments

Comments
 (0)