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.

Ordering results by relationship weights

chris23
Node Clone

Hi, I am currently writing a query to get results by path matching. The relationships between the nodes have weights and i want to sum them up and divide them by the number of relationships in one single path.

 

Match (s) where ID(s) = 0 Match (s)-[rel1:is_a]->(:GeneralPersonType)<-[rel2:is_related_to]-(:GeneralCourseType)<-[rel3:is_a]-(x:Course) with ....
 
So at the moment i am trying some things out but i always get the same value instead of 6 different ones i can order the courses (x) by.  What i want to do is calculate (rel1.weight + rel2.weight + rel3.weight) / 3 for each of my paths that lead to x.
1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

Can you post the actual query?

In the meantime, I will provide you with a potential approach you can apply. You can use the 'reduce' method to sum up the weights of each relationship along each path. Of course, change the match to meet your path requirements.

match p=()-[:REL*]->()
return reduce(sum=0, x in relationships(p) | sum + x.weight) as sum

 

View solution in original post

2 REPLIES 2

glilienfield
Ninja
Ninja

Can you post the actual query?

In the meantime, I will provide you with a potential approach you can apply. You can use the 'reduce' method to sum up the weights of each relationship along each path. Of course, change the match to meet your path requirements.

match p=()-[:REL*]->()
return reduce(sum=0, x in relationships(p) | sum + x.weight) as sum

 

It seems that my query worked just fine and i made a mistake with the weights. My original approach was:

 
Match (s) where ID(s) = 2 Match (s)-[rel1:is_a]->(:GeneralPersonType)
<-[rel2:is_related_to]-(:GeneralCourseType)<-[rel3:is_a]-(x:Course) 
with rel1.weight + rel2.weight + rel3.weight as result return result

I tried your query and it also works just fine for me so thank you very much for your quick reply!