Pra*_*aud 1 foreach batch-processing neo4j cypher
有人可以帮助 neo4j 新手(这是我的第二天!)解决以下问题吗?
我有一个数据库,其中包含所有玩过游戏会话 (S) 的玩家 (P) 以及他们获得的分数。有无数个S,我们称它们为S1、S2、S3…等。还有无数个P,P1、P2、P3…等。
每个会话都有玩家,例如
(P)-[:已播放]->(S)
每个会话都有不同数量的玩家,从 2 到 10 人不等。
我想做的是访问每个会话,获取该会话的每个玩家,然后根据分数对他们进行排名。首先需要将玩家按照得分排序,然后排名,每个得分高于前一个玩家的玩家具有 BEAT 关系。通常,我会使用 FOREACH 循环,但我不知道如何对 Cypher 执行相同的操作。
例如,S1 有玩家 P1、P3 和 P5。如果 P3 得到 100,P1 得到 70,P5 得到 30,我想创建以下关系:
(P3)-[:BEAT]->(P1)-[:BEAT]->(P5)
我需要为每个会话执行此操作。解决这个问题的最佳方法是什么?
问候,
假设score存储在:PLAYED关系上,这应该有效:
// Find all players who have played in a session
MATCH (p:Player)-[r:PLAYED]->(s:Session)
// for each Session, order the players by their score for that session
WITH s, p ORDER BY r.score DESC
// for each session, group the players (now ordered by their scores)
WITH s, COLLECT(p) AS players
// iterate over the sequence 0..number_of_players_in_this_session-2
UNWIND range(0,size(players)-2) AS i
// grab pairs of players, starting from the highest scoring
WITH players[i] AS l1, players[i+1] AS l2
// create :BEAT relationship
CREATE (l1)-[:BEAT]->(l2)
Run Code Online (Sandbox Code Playgroud)
这里有一个简单的 Neo4j 控制台示例
当然,这里存在数据建模问题,您没有将关系:BEAT与特定会话关联起来。
| 归档时间: |
|
| 查看次数: |
2712 次 |
| 最近记录: |