2

I do have relationship like following, through my Cypher query I'm beginning with Segment1 and getting relationships accordingly.

(Segment1)-[:Leaf]->(Op1)-[:Account]->(Ac2)-[:Parent]->(Acc1)-[:Parent]->(Acc)

I want to get the top most node of [:Parent] relationship hierarchy. Is there any way to obtain it?

Note: I need obtain this without using loops in Cypher because this is only a part of my query.

Expected Output

(Acc)

Please help me to figure out this

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
nad.SN
  • 77
  • 7

3 Answers3

1

I believe something like this should work:

MATCH ({name:'Segment1'})-[:Leaf|:Account|:Parent*]->(b)
WITH collect(b) as nodes
RETURN last(nodes)

This query start from a node with name = 'Segment1' and walk through relationships of types :Leaf, :Account and :Parent to b. After b is collected into a list with collect() function. Then the last node in the list is returned. This is done by the last() function.

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
1

This query will work:

MATCH (Segment1)-[:Leaf]->(Op1)-[:Account]->(Ac2)-[*:Parent]->(Acc)
WHERE NOT (Acc)-[:Parent]->()
RETURN Acc;

Unlike @logisma's solution, this query should be more performant, since it immediately ignores all Acc candidates that have an outgoing Parent relationship (instead of determining the size of the entire outgoing path).

cybersam
  • 63,203
  • 6
  • 53
  • 76
0

You should try this query :

MATCH (Segment1 {name:'Segment1'})-[:Leaf]->(Op1)-[:Account]->(Ac2)-[*:Parent]->(Acc)
WHERE size((Acc)-[:Parent]->()) = 0
RETURN Acc

Definition of the last [Parent] node is done by the WHERE clause.

Cheers

logisima
  • 7,340
  • 1
  • 18
  • 31