Search nodes then expand their neighborhood via BFS. Returns (nodes, relationships).
(
self,
query: str,
hops: int = 2,
limit: int = 50,
)
| 634 | return nodes |
| 635 | |
| 636 | def search_graph( |
| 637 | self, |
| 638 | query: str, |
| 639 | hops: int = 2, |
| 640 | limit: int = 50, |
| 641 | ) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]: |
| 642 | """Search nodes then expand their neighborhood via BFS. |
| 643 | |
| 644 | Returns (nodes, relationships). |
| 645 | """ |
| 646 | hops = max(0, min(hops, 5)) |
| 647 | |
| 648 | match_nodes = self.search_nodes(query, limit=limit) |
| 649 | if not match_nodes: |
| 650 | return [], [] |
| 651 | |
| 652 | node_map: dict[str, dict[str, Any]] = {} |
| 653 | rel_map: dict[str, dict[str, Any]] = {} |
| 654 | |
| 655 | for n in match_nodes: |
| 656 | node_map[n["id"]] = n |
| 657 | |
| 658 | if hops > 0: |
| 659 | for n in match_nodes: |
| 660 | traversal = self.traverse(n["id"], direction="both", max_depth=hops) |
| 661 | for t in traversal: |
| 662 | nid = t["node"]["id"] |
| 663 | if nid not in node_map: |
| 664 | node_map[nid] = t["node"] |
| 665 | rid = t["relationship"]["id"] |
| 666 | if rid not in rel_map: |
| 667 | rel_map[rid] = t["relationship"] |
| 668 | else: |
| 669 | # hops=0: return only relationships between matched nodes |
| 670 | for r in self.list_relationships_for_nodes(set(node_map.keys()), limit * 2): |
| 671 | rel_map[r["id"]] = r |
| 672 | |
| 673 | return list(node_map.values()), list(rel_map.values()) |
| 674 | |
| 675 | # -- traversal ------------------------------------------------------- |
| 676 |