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

46 views
Skip to first unread message

Vinny

unread,
Apr 29, 2012, 5:06:36 PM4/29/12
to 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.

Peter Neubauer

unread,
Apr 30, 2012, 1:58:44 AM4/30/12
to ne...@googlegroups.com
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

?

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j

Peter Neubauer

unread,
May 23, 2012, 7:45:19 AM5/23/12
to ne...@googlegroups.com
Vinny,
also added an example on your SO question here:

http://stackoverflow.com/questions/10710063/neo4j-searching-nodes-from-bottom-to-top-based-on-relationship/10716461#10716461

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j


Reply all
Reply to author
Forward
0 new messages