简体   繁体   中英

Neo4j return all nodes and relationships that exist in certain subgraph

I would like to specify a node and return all the relationships that the node has, along with every other relationships that are connecting the rest of the nodes.

An example of what I would like to return is:

Eric, PARENT_OF, Mia
Eric, PARENT_OF, Peter
Mia, SIBLING_OF, Peter
Mia, SPOUSE_OF, Mark
...

Assuming that the specified node is Eric , they query should return all the relationships that are directly connected to him along with the relationships that are not connected to Eric but are connected to some of his connected nodes.

I have simulated your scenario with this data set:

CREATE (eric:Person {name:'Eric'})
CREATE (mia:Person {name:'Mia'})
CREATE (peter:Person {name:'Peter'})
CREATE (mark:Person {name:'Mark'})
CREATE (eric)-[:PARENT_OF]->(mia)
CREATE (eric)-[:PARENT_OF]->(peter)
CREATE (mia)-[:SIBLING_OF]->(peter)
CREATE (mia)-[:SPOUSE_OF]->(mark)

The below query should work to achieve your goal:

MATCH (root:Person {name:'Eric'})-[r*1..3]->(a:Person)
UNWIND r AS rs
RETURN DISTINCT startNode(rs).name, type(rs), endNode(rs).name

As result:

╒════════════════════╤════════════╤══════════════════╕
│"startNode(rs).name"│"type(rs)"  │"endNode(rs).name"│
╞════════════════════╪════════════╪══════════════════╡
│"Eric"              │"PARENT_OF" │"peter"           │
├────────────────────┼────────────┼──────────────────┤
│"Eric"              │"PARENT_OF" │"Mia"             │
├────────────────────┼────────────┼──────────────────┤
│"Mia"               │"SIBLING_OF"│"peter"           │
├────────────────────┼────────────┼──────────────────┤
│"Mia"               │"SPOUSE_OF" │"Mark"            │
└────────────────────┴────────────┴──────────────────┘

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM