KoalaD
KoalaD

Reputation: 357

Traversing the graphdb to the nth degree

I'm at a loss.

Scenario: get depth of 2 from Joe (#'s represent 'Person'. Letters represent 'Position')

    0              E
    |              |
    1              B 
 /  |  \         / | \
2  JOE  3       C  A  D
   /|\            /|\
  0 0 0          F G H
 /\ | |         /\ | | 
0 0 0 0        I J K L

Catch is, a person is tied to a position. Position has relationships to each other but a person doesn't have a relationship to another person. so it goes something like:

Joe<-[:occupied_by]-(PositionA)-[:authority_held_by]->
(PositionB)-[:occupied_by]->Sam

This query:

Match (:Identity {value:"1234"})-[:IDENTIFIES]->(posStart:Position)
-[:IS_OCCUPIED_BY]->(perStart:Person) 
Optional Match p=(perStart)<-[:IS_OCCUPIED_BY]-(posStart)
-[r:AUTHORITY_HELD_BY*..1]-(posEnd:Position)-[:IS_OCCUPIED_BY]->
(perEnd:Person) Return p

does get me what I need but always returns the first column as the original node it started with (perStart). I want to return it in a way where the first two column always represent the start node and the second column to represent the end node.

PositionA, PositionB  (and we can infer this means A-[:authority_held_by]->B

If we had bi directional relationship, such as, A-[:authority_held_by]->B and B-[:manages]->A I wouldn't mind what's in the first or the second column as we can have the third column represent the relationship

PositionB, PositionA, [:manages]

but we are trying to stay away from bi-directional relationship

Ultimately I want something like:

PositionA, PositionB   (inferring, A-[:A_H_B]->B)
PositionB, PositionE   (inferring, B-[:A_H_B]->E)
PositionF, PositionA   (inferring, F-[:A_H_B]->A)
PositionG, PositionA   (inferring, G-[:A_H_B]->A)

Is this possible with cypher or do I have to do some black magic? :)

I hope I explained throughly and understandably.. Thank you so much in advance!

Upvotes: 1

Views: 318

Answers (1)

Dan G
Dan G

Reputation: 1118

would replacing Return p with-

RETURN nodes(p)[0] AS START, LAST(nodes(p)) as last 

work?

Upvotes: 1

Related Questions