vinnylinux
vinnylinux

Reputation: 7024

Describing a Cypher Query for multi-level relationship in Neo4j?

I'm developing a small affiliate structure to understand the concept of graph databases better, as well learn Neo4J and see what it can offer me. I've been with RDBMS for years now and Cypher is pretty rough. I'm trying to build a very simple affiliate system:

Affiliate Joe has referred Mary, Bob and Mark. So, i create all their nodes and create the "referred" relationship. Now Mary refers Julie, Jessica and Joan. Bob refers Billy and Baxter. Mark refers Michael and Marx. And their referrals keep referring people.

For each referral that one of Joe's original referrals, Joe earns a "generation". His first generation is Mary, Bob and Mark. His second generation is Julie, Jessica, Joan, Billy, Baxter, Michael and Marx.

Now, with a Cypher query, how can i discover his generations and, of course, discover their number? Their place in the tree? How can i know who is from his 3rd or 4th generation, and who they are?

My mind is twisting here, hope you guys can help.

Upvotes: 2

Views: 2026

Answers (1)

Peter Neubauer
Peter Neubauer

Reputation: 6331

Vinny, look at http://tinyurl.com/7vryzwz, is this what you are lookad for, basically

START referrer=node(1) 
MATCH path=referrer-[:referred*1..]->refferee 
RETURN referrer,refferee, length(path) as generation 
ORDER BY length(path) asc 

Upvotes: 3

Related Questions