简体   繁体   English

在关系 NEO4j 中添加属性

[英]Adding up a property in the relationship NEO4j

I am trying to add up properties in the relationship along a path.我正在尝试沿路径添加关系中的属性。 This is my first week with NEO4j and I am now stuck.这是我使用 NEO4j 的第一周,现在我被卡住了。 If anyone can advise.如果有人可以建议。 Thank you谢谢

match (dbn:City {name: 'Durban'})
match (unb:City {name: 'Pinetown'})
match path = shortestpath((dbn)-[dist:DISTANCE_TO*]->(unb))
return path, sum(dist.distance)

This path has a total of 4 nodes each with a different distance total of 19kms.这条路径共有 4 个节点,每个节点的距离不同,总距离为 19 公里。

it gives an error of Type Mismatch expected Map but got list它给出了类型不匹配预期的错误 Map 但得到了列表

Here is the code I did to generate the Graph这是我为生成图表所做的代码

        merge  (c:Country {name: $Country}) 
            merge  (r:Region {name: $Region})   
            merge  (c1:City {name: $cityfrom})
            merge  (c2:City {name: $cityto})
            merge  (r)-[:IS_IN] -> (c)
            merge  (c1)-[:DISTANCE_TO {distance:toInteger($distance)}] -> (c2
            merge  (c2)-[:DISTANCE_TO {distance:toInteger($distance)}] -> (c1)
            merge  (c1)-[:IS_IN] -> (r)
            merge  (c2)-[:IS_IN] -> (r)

toInteger is your friend. toInteger是你的朋友。

            merge  (r:Region {name: $Region})   
            merge  (c1:City {name: $cityfrom})
            merge  (c2:City {name: $cityto})
            merge  (r)-[:IS_IN] -> (c)
            merge  (c1)-[:DISTANCE_TO {distance:toInteger($distance)}] -> (c2)
            merge  (c2)-[:DISTANCE_TO {distance:toInteger($distance)}] -> (c1)
            merge  (c1)-[:IS_IN] -> (r)
            merge  (c2)-[:IS_IN] -> (r)

Given a path variable, you can use relationships(path) to get the list of relationships for it.给定一个path变量,您可以使用relationships(path)来获取它的关系列表。

You can't use sum() on the path or the list of relationships, since sum() is an aggregation function that applies over multiple rows, not a scaler function that applies to a list.您不能在路径或关系列表上使用sum() ,因为sum()是适用于多行的聚合 function,而不是适用于列表的缩放器 function。

You have some options.你有一些选择。

If you UNWIND the relationships list to rows, then you can use sum() :如果您将关系列表展开为行,则可以使用sum()

match (dbn:City {name: 'Durban'})
match (unb:City {name: 'Pinetown'})
match path = shortestpath((dbn)-[dist:DISTANCE_TO*]->(unb))
UNWIND relationships(path) as rel
return path, sum(rel.distance) as sum

You can install APOC Procedures and use apoc.coll.sum() to sum the elements of the collection:您可以安装 APOC 程序并使用apoc.coll.sum()对集合的元素求和:

match (dbn:City {name: 'Durban'})
match (unb:City {name: 'Pinetown'})
match path = shortestpath((dbn)-[dist:DISTANCE_TO*]->(unb))
return path, apoc.coll.sum([rel IN relationships(path) | rel.distance]) as sum

You can use the reduce() function to sum the distances:您可以使用reduce() function 对距离求和:

match (dbn:City {name: 'Durban'})
match (unb:City {name: 'Pinetown'})
match path = shortestpath((dbn)-[dist:DISTANCE_TO*]->(unb))
return path, reduce(sum=0, rel IN relationships(path) | sum + rel.distance) as sum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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