Skip to content

Commit 3394f86

Browse files
committed
feature #235 [Store][Pg] Add support for custom "where" expression (lyrixx)
This PR was merged into the main branch. Discussion ---------- [Store][Pg] Add support for custom "where" expression | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Docs? | no | Issues | | License | MIT I want to be able to filter some record before comparison. Usage: ```php $crawlAId = '013e951c-335f-4e2b-9414-9eedbf0f51df'; $crawlBId = '396af6fe-0dfd-47ed-b222-3dbcced3f38e'; $rows = $connection ->executeQuery(<<<SQL SELECT * FROM {$tableName} WHERE metadata->>'crawlId' = '{$crawlAId}' SQL) ->fetchAllAssociative() ; foreach ($rows as $i => $row) { $vector = new Vector(json_decode($row['embedding'], true)); $documents = $store->query( $vector, [ 'maxScore' => 0.1, 'where' => "metadata->>'crawlId' = :crawlId AND id != :currentId", 'params' => [ 'crawlId' => $crawlBId, 'currentId' => $row['id'], ], ], ); if (!$documents) { continue; } $metadata = json_decode($row['metadata'], true, 512, \JSON_THROW_ON_ERROR); echo "Current document: {$metadata['url']}\n"; foreach ($documents as $i => $document) { echo "- {$document->metadata['url']} (score: {$document->score})\n"; if ($i >= 1) { break; } } echo "\n"; } ``` Commits ------- 3bdcb79 [Store][Pg] Add support for custom "where" expression
2 parents f40fcda + 3bdcb79 commit 3394f86

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)