You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在py2neo中获取指定节点的关联节点及关系(不使用Cypher)

Hey there! I’ve been tinkering with py2neo and Neo4j myself recently, so I totally get wanting to skip raw Cypher queries when you can stick to the Python API. Let me walk you through how to fetch all connected nodes and their relationships using the NodeSelector you already know, plus some other handy py2neo methods.

Getting Connected Nodes & Relationships with py2neo (No Raw Cypher Required)

First, let's recap grabbing your target node since you already mentioned using NodeSelector:

from py2neo import Graph, NodeSelector

# Connect to your Neo4j instance
graph = Graph("bolt://localhost:7687", auth=("neo4j", "your_password_here"))
selector = NodeSelector(graph)

# Fetch your target node (example: a User node with email "jane.doe@example.com")
target_node = selector.select("User", email="jane.doe@example.com").first()

Once you have that target node, py2neo gives you built-in methods to access its connections without writing any Cypher.

Retrieve All Connected Nodes & Relationships

The match() method on a Node object will return all relationships (both incoming and outgoing) linked to that node. You can iterate through these relationships to extract the connected node and relationship details:

# Get all relationships attached to the target node
all_relationships = list(target_node.match())

# Loop through each relationship to get connected nodes and metadata
for rel in all_relationships:
    # Determine which end of the relationship is the connected node (not the target)
    connected_node = rel.end_node if rel.start_node == target_node else rel.start_node
    
    # Print out useful info (adjust properties based on your node labels)
    print(f"Connected Node: {connected_node.get('name', 'Unnamed Node')}")
    print(f"Node Label(s): {list(connected_node.labels)}")
    print(f"Relationship Type: {rel.type}")
    print(f"Relationship Properties: {dict(rel)}")
    print("---")

Filter by Relationship Type

If you only care about specific relationship types (like "FOLLOWS" or "WORKS_AT"), pass the type(s) to the match() method:

  • Fetch single relationship type:
    # Only get outgoing "FOLLOWS" relationships
    follow_connections = list(target_node.match(r_type="FOLLOWS"))
    
  • Fetch multiple relationship types at once:
    multi_type_connections = list(target_node.match(r_type=["FOLLOWS", "WORKS_AT"]))
    

Separate Incoming vs Outgoing Relationships

If you need to distinguish between connections coming into the node vs going out, use these dedicated methods:

  • Get all outgoing relationships (target node is the start of the relationship):
    outgoing_connections = list(target_node.out_relationships())
    
  • Get all incoming relationships (target node is the end of the relationship):
    incoming_connections = list(target_node.in_relationships())
    

A Quick Note

These methods all use py2neo's internal abstraction of Cypher, so you don't have to write any raw queries yourself. It's a clean, Pythonic way to work with your graph data.

内容的提问来源于stack exchange,提问作者harshvardhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:05:55