cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Getting started with a graph traversal and multiplication

I'm just getting started with Cypher and having a little trouble thinking about how to structure the query that I want.

Essentially, I start with a node (node A, we'll call it). Node A has an amount and is linked to Node B by Relationship of type 1. Node B is linked to Node C by Relationship of type 2, which has certain properties that may or may not match the properties of Node A, as well as a percentage property. As I go to Node C, I would like to multiply the amount from Node A by the matching relationship's percentage to get the relative amount at Node C. I would like to repeat this as I go to Node D, which is linked to Node C by Relationship 3, which has similar properties and also a percentage associated with it. I'm trying to figure out how I can write a query that will take the original amount and multiply it up the chain by matching the properties of each relationship.

Any pointers? I'm having trouble wrapping my brain around this new way of thinking, being used to relational databases, where I would do some kind of recursive query to calculate the effective percentage and multiply it by the original amount.

3 REPLIES 3

Not sure I fully grasped the logic of what you're trying to build, but it sounds like you might need to match each (n)-[r]-(n) separately, and use the WITH clause to keep passing the calculated value up the chain:

MATCH (a:Node)-[r:TYPE_1]->(b:Node)-[r2:TYPE_2]->(c:Node)
WITH a.amount * r2.percentage AS relAmtAtoC, c
MATCH (c)-[r3:TYPE_3]->(d:Node)
WITH relAmtAtoC * r3.percentage as relAmtAtoD, d
MATCH...

Thanks for the quick response, Terry! I'm pretty sure we're on the same page. It gets more complicated. I don't know how far up the chain I'll need to go ahead of time - is there a way to recursively do what you're doing with the WITH statement, i.e., start at one node, and keep progressing to an indeterminate ending node while multiplying up the chain? Instead of having TYPE_3, all the relationships up the chain past b will be TYPE_2. I'd also like to match the properties of a to the properties of the TYPE_2 relationship as I flow up.

balmix
Node Link

I'm also interested in a similar question of graph traversal control, if anyone can answer him.
I haven't been able to find resources regarding this besides going into the java back-end of the graph traversal.

Especially interested in the "recursive" part.

Essentially: "traverse the graph from the starting point given some 'order by', and the ending will be decided on the fly by a limited cumulative distance". This could mean 0 node traversed or 1000 depending on the distance found along the way.