Neo4j聚合动态值

时间:2018-05-24 09:39:12

标签: neo4j cypher

我正在研究neo4j的APOC程序。我无法获得下面查询返回的所有节点的距离平均值。

match(n:Company)  
WHERE NOT (n)-[:refersTo]->() and n.name starts with "google" 
with n as company, apoc.text.distance('google', n.name_lower) as distance  
return company.name, distance, avg(distance)

距离和平均值(距离)的值始终相同。下面是结果样本的附图。

enter image description here

编辑1: 尝试了建议的查询:

match(n:Company)  
WHERE NOT (n)-[:refersTo]->() and n.name_lower starts with "google" 
with n as company, apoc.text.distance('google', n.name_lower) as distance  
with company, collect(distance) as distances, avg(distance) as distAvg
unwind distances as distance
return company.name as name, distance, distAvg

得到了相同的结果。

enter image description here

2 个答案:

答案 0 :(得分:1)

此查询将返回名称/距离对的集合以及所有距离的平均值:

MATCH (n:Company)  
WHERE NOT (n)-[:refersTo]->() AND n.name_lower starts with "google"
WITH n.name AS name, apoc.text.distance('google', n.name_lower) AS distance
RETURN COLLECT({name: name, distance: distance}) as data, AVG(distance) as distAvg;

以下是一个示例结果:

╒══════════════════════════════════════════════════════════════════════╤═════════╕
│"data"                                                                │"distAvg"│
╞══════════════════════════════════════════════════════════════════════╪═════════╡
│[{"name":"Google Inc.","distance":5},{"name":"Google Max","distance":4│4.5      │
│}]                                                                    │         │
└──────────────────────────────────────────────────────────────────────┴─────────┘

答案 1 :(得分:0)

Neo4j中的聚合与非聚合列有关。 在您的查询中,对于每个公司及其距离,您将获得该单一距离值的平均值。

您需要折叠或删除setTimeout值(因此平均值相对于所有距离)。试试这个:

E:\reactnative\cd webview

E:\reactnative\webview>react-native run-ios
Scanning folders for symlinks in E:\reactnative\webview\node_modules (38ms)
Found Xcode project webview.xcodeproj

spawnSync xcrun ENOENT

E:\reactnative\webview>

修改

正如cybersam所指出的,这是获得每家公司的平均距离而不是整体。我将这个作为聚合如何工作的例子并且可能很棘手。 Cyber​​sam的回答提供了正确的聚合方式。