简体   繁体   中英

Neo4j get edges between 1st degree nodes

Assume i have graph relations defined as A->B, A->D, C->A, B->C, B->D. I need to get the following subgraph in Neo4j -

  1. Get all 1st degree connections (indegree or outdegree) of a node ie For node A, it would be B, C, D
  2. Get all the edges between these 1st degree nodes. Since B,C,D are 1st degree connections, edges would be B->C, B->D

For the 1st part, I have the following query -

MATCH (s:Node)->(d:Node) 
WHERE s.name = 'A' OR d.name = 'A'

I'm not able to get the 2nd part of the data in the same query. Do i need to iterate through all the nodes?

This is a simple pattern that says that you start the path from the node, then go to the node next to it, then one of which you can return to the starter node:

MATCH (A:Node {name:'A'}) WITH A
MATCH (A)--(FD1:Node)-[r]-(FD2:Node)--(A) 
  WHERE ID(FD1) > ID(FD2)
RETURN FD1, r, FD2

PS And remember that you can not specify the direction of the relationship.

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