fix: stabilize metadata filters on postgres (#536) · basicmachines-co/basic-memory@009e849 · GitHub
Skip to content

Commit 009e849

Browse files
authored
fix: stabilize metadata filters on postgres (#536)
Signed-off-by: phernandez <paul@basicmachines.co>
1 parent 8838571 commit 009e849

3 files changed

Lines changed: 20 additions & 15 deletions

File tree

src/basic_memory/repository/metadata_filters.py

Lines changed: 12 additions & 4 deletions

src/basic_memory/repository/postgres_search_repository.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,22 +332,19 @@ async def search(
332332
like_param_single = f"{base_param}_{j}_like_single"
333333
params[like_param_single] = f"%'{val}'%"
334334
tag_conditions.append(
335-
f"({json_expr} @> :{tag_param}::jsonb "
335+
f"({json_expr} @> CAST(:{tag_param} AS jsonb) "
336336
f"OR {text_expr} LIKE :{like_param} "
337337
f"OR {text_expr} LIKE :{like_param_single})"
338338
)
339339
conditions.append(" AND ".join(tag_conditions))
340340
continue
341341

342342
if filt.op in {"gt", "gte", "lt", "lte", "between"}:
343-
if filt.comparison == "numeric":
344-
numeric_expr = (
345-
f"CASE WHEN ({text_expr}) ~ '^-?\\\\d+(\\\\.\\\\d+)?$' "
346-
f"THEN ({text_expr})::double precision END"
347-
)
348-
compare_expr = numeric_expr
349-
else:
350-
compare_expr = text_expr
343+
compare_expr = (
344+
f"({metadata_expr} #>> '{path}')::double precision"
345+
if filt.comparison == "numeric"
346+
else text_expr
347+
)
351348

352349
if filt.op == "between":
353350
min_param = f"meta_val_{idx}_min"

tests/repository/test_metadata_filters.py

Lines changed: 2 additions & 2 deletions

0 commit comments

Comments
 (0)