From bdb1ae0786ae204c6e9181c7334e93f7c3f64fb2 Mon Sep 17 00:00:00 2001 From: GGrassia Date: Fri, 10 Oct 2025 11:05:17 +0200 Subject: [PATCH] feat (metadata postgres): added logic for IN clauses on operands --- lightrag/kg/postgres_impl.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lightrag/kg/postgres_impl.py b/lightrag/kg/postgres_impl.py index 386b5e87..afbc407d 100644 --- a/lightrag/kg/postgres_impl.py +++ b/lightrag/kg/postgres_impl.py @@ -2150,12 +2150,18 @@ class PGVectorStorage(BaseVectorStorage): return "" def build_single_condition(key, value): - if isinstance(value, (list, dict)): - # Use GIN-optimized containment operator - json_value = json.dumps(value) - return f"metadata @> '{{\"{ key}\" : {json_value}}}'" + #Check and build "IN" conditions for the operand + if isinstance(value, list): + if not value: + return "1=0" + + in_conditions = [ + f"metadata @> '{{\"{key}\": {json.dumps(v)}}}'" for v in value + ] + return f"({ ' OR '.join(in_conditions) })" + else: - # Use containment for scalars too - faster with GIN index + # Use for scalars and dictionaries json_value = json.dumps(value) return f"metadata @> '{{\"{ key}\" : {json_value}}}'"