Directly evaluating a MetadataFilters instance on a BaseNode instance #17574
Unanswered
ZmeiGorynych
asked this question in
Q&A
Replies: 1 comment 2 replies
-
Yes, there is a utility function called from llama_index.core.vector_stores import MetadataFilters
from llama_index.core.schema import BaseNode
from typing import List
def filter_nodes(nodes: List[BaseNode], filters: MetadataFilters):
filtered_nodes = []
for node in nodes:
matches = True
for f in filters.filters:
if f.key not in node.metadata:
matches = False
continue
if f.value != node.metadata[f.key]:
matches = False
continue
if matches:
filtered_nodes.append(node)
return filtered_nodes This function should help you filter nodes based on the metadata filters you have [1][2]. To continue talking to Dosu, mention @dosu. Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I have an instance of
MetadataFilters
and aList[BaseNode]
and would like to select those nodes from that list that fit the filter. Is there already a function to do that? I'd think there would be adef MetadataFilters.matches(node: BaseNode) -> bool
method, but haven't been able to find anything like that.Reluctant to write my own as it'd have to support arbitrarily deep nested filters, etc.
Beta Was this translation helpful? Give feedback.
All reactions