Skip to content

Commit

Permalink
Support pulling attribute data in graph.scan, closes #849
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmezzetti committed Jan 9, 2025
1 parent 80db3a9 commit 4e5331e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
13 changes: 8 additions & 5 deletions src/python/txtai/graph/networkx.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ def create(self):
def count(self):
return self.backend.number_of_nodes()

def scan(self, attribute=None):
# Nodes containing an attribute
def scan(self, attribute=None, data=False):
# Full graph
graph = self.backend

# Filter graph to nodes having a specified attribute
if attribute:
return nx.subgraph_view(self.backend, filter_node=lambda x: attribute in self.node(x))
graph = nx.subgraph_view(self.backend, filter_node=lambda x: attribute in self.node(x))

# Return all nodes
return self.backend
# Return either list of matching ids or tuple of (id, attribute dictionary)
return graph.nodes(data=True) if data else graph

def node(self, node):
return self.backend.nodes.get(node)
Expand Down
9 changes: 5 additions & 4 deletions src/python/txtai/graph/rdbms.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ def create(self):
# Return NetworkX compatible backend
return self.graph.nx

def scan(self, attribute=None):
def scan(self, attribute=None, data=False):
if attribute:
for node in self.backend:
if attribute in self.node(node):
yield node
attributes = self.node(node)
if attribute in attributes:
yield (node, attributes) if data else node
else:
yield from super().scan(attribute)
yield from super().scan(attribute, data)

def load(self, path):
# Create graph instance
Expand Down

0 comments on commit 4e5331e

Please sign in to comment.