Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix: ensure all metadata filters are processed in AND logic (#13019)

Clint-chana68c56def7b3
diff --git a/common/metadata_utils.py b/common/metadata_utils.py
index 8403dd9e06..c919bd186a 100644
--- a/common/metadata_utils.py
+++ b/common/metadata_utils.py
@@ -138,20 +138,24 @@ def meta_filter(metas: dict, filters: list[dict], logic: str = "and"):
ids.extend(docids)
return ids
- for k, v2docs in metas.items():
- for f in filters:
- if k != f["key"]:
- continue
+ for f in filters:
+ k = f["key"]
+ if k not in metas:
+ # Key not found in metas: treat as no match
+ ids = []
+ else:
+ v2docs = metas[k]
ids = filter_out(v2docs, f["op"], f["value"])
- if not doc_ids:
- doc_ids = set(ids)
+
+ if not doc_ids:
+ doc_ids = set(ids)
+ else:
+ if logic == "and":
+ doc_ids = doc_ids & set(ids)
+ if not doc_ids:
+ return []
else:
- if logic == "and":
- doc_ids = doc_ids & set(ids)
- if not doc_ids:
- return []
- else:
- doc_ids = doc_ids | set(ids)
+ doc_ids = doc_ids | set(ids)
return list(doc_ids)