Cypher查询:将聚合值指定为关系的属性

时间:2018-01-15 13:59:38

标签: neo4j cypher

this question之后,我有以下玩具示例:

CREATE (A1:Worker {ref:"A1"})
CREATE (A2:Worker {ref:"A2"})

CREATE (B1:Worker {ref:"B1"})
CREATE (B2:Worker {ref:"B2"})

CREATE (A1)-[:StreamsTo {type:"stream1"}]->(B1)
CREATE (A1)-[:StreamsTo {type:"stream2"}]->(B1)
CREATE (A1)-[:StreamsTo {type:"stream3"}]->(B1)
CREATE (A1)-[:StreamsTo {type:"stream1"}]->(B2)
CREATE (A1)-[:StreamsTo {type:"stream2"}]->(B2)

CREATE (A2)-[:StreamsTo {type:"stream1"}]->(B1)
CREATE (A2)-[:StreamsTo {type:"stream1"}]->(B2)
CREATE (A2)-[:StreamsTo {type:"stream2"}]->(B2);

这会生成以下图表:

graph of toy example

我想要做的是计算离开源节点的每种流类型的路由概率(RP),即1 /该类型的传出关系的数量。我可以使用此查询计算RP值:

MATCH (source:Worker)-[st:StreamsTo]->(:Worker) 
RETURN source.ref as Worker, 
       st.type as StreamType, 
       COUNT(st) as OutgoingCount,
       1.0 / COUNT(st) as RP
ORDER BY source.ref, st.type;

结果是:

╒════════╤════════════╤═══════════════╤════╕
│"Worker"│"StreamType"│"OutgoingCount"│"RP"│
╞════════╪════════════╪═══════════════╪════╡
│"A1"    │"stream1"   │2              │0.5 │
├────────┼────────────┼───────────────┼────┤
│"A1"    │"stream2"   │2              │0.5 │
├────────┼────────────┼───────────────┼────┤
│"A1"    │"stream3"   │1              │1   │
├────────┼────────────┼───────────────┼────┤
│"A2"    │"stream1"   │2              │0.5 │
├────────┼────────────┼───────────────┼────┤
│"A2"    │"stream2"   │1              │1   │
└────────┴────────────┴───────────────┴────┘

所以我现在需要做的就是为每个源节点(Worker)和流类型组合计算RP值,并将该值添加为离开给定源节点的该类型的每个关系的属性。

我知道我可以使用WITH将上述查询的输出传递给新查询。但是,我坚持如何将聚合RP值应用于具有给定属性的给定源的所有关系?我在cypher中看到你可以将结果收集到地图中,但我不确定如何在查询中使用它?

我可以在应用程序端执行此操作,但显然更愿意在可能的情况下执行此服务器端。

1 个答案:

答案 0 :(得分:0)

以下是使用collect()UNWIND的解决方案:

MATCH (source:Worker)-[st:StreamsTo]->(:Worker) 
// do the calculations, collection st relations into sts array
WITH collect(st) as sts, source.ref as Worker, 
       st.type as StreamType, 
       COUNT(st) as OutgoingCount,
       1.0 / COUNT(st) as RP

// expanding sts list to st rows
UNWIND sts as st
// set the property sr.RP = RP
set st.RP = RP

// collect st again to avoid a cartesian product
WITH collect(st) as sts, Worker, StreamType, OutgoingCount, RP

// return desired data, ordering
return Worker, StreamType, OutgoingCount, RP
ORDER BY Worker, StreamType