简体   繁体   中英

Neo4j 2.0 - summing relationship results from cypher query

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM