Search code examples
pythonpython-3.xneo4jcypherpy2neo

Get the nodes which are all connected with my input node using py2neo and flask


I want to fetch the connected nodes from ny neo4j database. enter image description here

For example If I give the input as 2 and then I have to fetch 1,3,4 and 5.I tried to explore the question but answer is related to neo4j only. I need a query with py2neo. is there anyway i can get it?

I tried this How to get all nodes connected to one node in neo4j graph in py2neo How to get all connected nodes in neo4j graph in py2neo

But these are all with neo4j not with py2neo


Solution

  • Suppose your nodes have a property called nodeid, you can use NodeMatcher() to match node 2 (see Node Matching), then iterate over its adjacent nodes:

    from py2neo import Graph, NodeMatcher
    
    matcher = NodeMatcher(graph)
    
    node = matcher.match(nodeid="2").first()
    
    list(r.end_node["nodeid"] for r in graph.match(nodes=(node,)))
    

    Otherwise, just run a cypher query:

    q = '''MATCH (a)-[r]-(b) where a.nodeid='2' RETURN b'''
    [i for i in graph.run(q)]