Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to Traverse neo4j Path using Cypher for different relationships

Tags:

neo4j

cypher

I want to Traverse a PATH in neo4j (preferably using Cypher, but I can write neo4j managed extensions).

Problem - For any starting node (:Person) I want to traverse hierarchy like

(me:Person)-[:FRIEND|:KNOWS*]->(newPerson:Person)

if the :FRIEND outgoing relationship is present then the path should traverse that, and ignore any :KNOWS outgoing relationships, if :FRIEND relationship does not exist but :KNOWS relationship is present then the PATH should traverse that node.

Right now the problem with above syntax is that it returns both the paths with :FRIEND and :KNOWS - I am not able to filter out a specific direction based on above requirement.

like image 923
msameep Avatar asked Oct 23 '25 17:10

msameep


1 Answers

1. Example data set

For the ease of possible further answers and solutions I note my graph creating statement:

CREATE
  (personA:Person {name:'Person A'})-[:FRIEND]->(personB:Person {name: 'Person B'}),
  (personB)-[:FRIEND]->(personC:Person {name: 'Person C'}),
  (personC)-[:FRIEND]->(personD:Person {name: 'Person D'}),
  (personC)-[:FRIEND]->(personE:Person {name: 'Person E'}),
  (personE)-[:FRIEND]->(personF:Person {name: 'Person F'}),
  (personA)-[:KNOWS]->(personG:Person {name: 'Person G'}),
  (personA)-[:KNOWS]->(personH:Person {name: 'Person H'}),
  (personH)-[:KNOWS]->(personI:Person {name: 'Person I'}),
  (personI)-[:FRIEND]->(personJ:Person {name: 'Person J'});

graph 1

2. Scenario "Optional Match"

2.1 Solution

MATCH (startNode:Person {name:'Person A'})
OPTIONAL MATCH friendPath = (startNode)-[:FRIEND*]->(:Person)
OPTIONAL MATCH knowsPath = (startNode)-[:KNOWS*]->(:Person)
RETURN friendPath, knowsPath;

If you do not need every path to all nodes of the entire path, but only the whole, I recommend using shortestPath() for performance reasons.

2.1 Result

Note the missing node 'Person J', because it owns a FRIENDS relationship to node 'Person I'.

graph 2

3. Scenario "Expand paths"

3.1 Solution

Alternatively you could use the Expand paths functions of the APOC user library. Depending on the next steps of your process you can choose between the identification of nodes, relationships or both.

MATCH (startNode:Person {name:'Person A'})
CALL apoc.path.subgraphNodes(startNode,
  {maxLevel: -1, relationshipFilter: 'FRIEND>', labelFilter: '+Person'}) YIELD node AS friendNodes
CALL apoc.path.subgraphNodes(startNode,
  {maxLevel: -1, relationshipFilter: 'KNOWS>', labelFilter: '+Person'}) YIELD node AS knowsNodes
WITH
  collect(DISTINCT friendNodes.name) AS friendNodes,
  collect(DISTINCT knowsNodes.name) AS knowsNodes
RETURN friendNodes, knowsNodes;

3.2 Explanation

  • line 1: defining your start node based on the name
  • line 2-3: Expand from the given startNode following the given relationships (relationshipFilter: 'FRIEND>') adhering to the label filter (labelFilter: '+Person').
  • line 4-5: Expand from the given startNode following the given relationships (relationshipFilter: 'KNOWS>') adhering to the label filter (labelFilter: '+Person').
  • line 7: aggregates all nodes by following the FRIEND relationship type (omit the .name part if you need the complete node)
  • line 8: aggregates all nodes by following the KNOWS relationship type (omit the .name part if you need the complete node)
  • line 9: render the resulting groups of nodes

3.3 Result

╒═════════════════════════════════════════════╤═════════════════════════════════════════════╕
│"friendNodes"                                │"knowsNodes"                                 │
╞═════════════════════════════════════════════╪═════════════════════════════════════════════╡
│["Person A","Person B","Person C","Person E",│["Person A","Person H","Person G","Person I"]│
│"Person D","Person F"]                       │                                             │
└─────────────────────────────────────────────┴─────────────────────────────────────────────┘
like image 164
ThirstForKnowledge Avatar answered Oct 26 '25 05:10

ThirstForKnowledge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!