2
votes

new to Neo4j.

I have some nodes connected by relationships that have a "weight" property with a number. I am trying to get an average of the weight properties for all relationships returned in the query.

Here is the query I'm trying:

MATCH (x)-[r*1..6]-prod:Product 
WHERE x.name = 'ddbrown::default' 
RETURN sum(r.weight);

I get the following exception

CypherTypeException: Expected `r` to be a Map but it was a Collection<relationship>

I'm obviously misinterpreting what's getting returned as r, but i'm not sure how to get a named pointer to each relationship.

Thanks for any help! Dean

1

1 Answers

5
votes

Sum can be used to aggregate over multiple matched return values. What you need to do is compute a value over multiple parts of the paths r matched. To compute values on collections you can use reduce

match x -[r*1..6]-prod:Product
WHERE x.name = 'ddbrown::default' 
return x, reduce(acc=0, n in r: acc + n.weight)/length(r)

On a different tangent, you are matching all possible relationships types and relationships in both directions in your query. You might want to restrict that to special and directed relationsships types.