cancel
Showing results forΒ
Did you mean:Β

## Ordering results by relationship weights

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
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``````

2 REPLIES 2
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``````

Node Clone

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!

Nodes 2022

NODES 2022, Neo4j Online Education Summit

OnΒ November 16 and 17 for 24 hours across all timezones, youβll learn about best practices for beginners and experts alike.

Neo4j Resources