Skip to content

Commit 10b4c60

Browse files
authored
Fix PostgresDbClient GetSimilarAsync minSimilarity requirement (#1057)
## Motivation and Context (Why the change? What's the scenario?) Since #684 The PostgresDbClient will fail to return results that match the minSimilarity requirement when multiple filters are used. This is due to how the ```WHERE``` clause is prepared: ```filter1 OR filter2 OR filter3 AND embedding <=> @Embedding < @maxDistance``` which cannot work as expected since the `AND` operator takes precedence over the `OR` operator ## High level description (Approach, Design) Simply add parenthesis around the filters argument
1 parent 164c159 commit 10b4c60

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

extensions/Postgres/Postgres/Internals/PostgresDbClient.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,13 +416,13 @@ DO UPDATE SET
416416

417417
// Filtering logic, including filter by similarity
418418
filterSql = filterSql?.Trim().Replace(PostgresSchema.PlaceholdersTags, this._colTags, StringComparison.Ordinal);
419-
if (string.IsNullOrWhiteSpace(filterSql))
420-
{
421-
filterSql = "TRUE";
422-
}
423419

424420
var maxDistance = 1 - minSimilarity;
425-
filterSql += $" AND {this._colEmbedding} <=> @embedding < @maxDistance";
421+
422+
var distanceFilter = $"{this._colEmbedding} <=> @embedding < @maxDistance";
423+
filterSql = string.IsNullOrWhiteSpace(filterSql)
424+
? distanceFilter
425+
: $"({filterSql}) AND {distanceFilter}";
426426

427427
if (sqlUserValues == null) { sqlUserValues = []; }
428428

0 commit comments

Comments
 (0)