Neo4j 获取按接近度排序的节点,并在同一级别按日期排序
我对neo4j非常陌生,我很难为我的查询得到一个好的结果。我有下一个型号:Neo4j 获取按接近度排序的节点,并在同一级别按日期排序,neo4j,cypher,Neo4j,Cypher,我对neo4j非常陌生,我很难为我的查询得到一个好的结果。我有下一个型号: Player <- HAS_PLAYERS - Game Node Player: playerId, name,...etc Node Game: gameId, gameDate Rel. HAS_PLAYERS: result 说明: PlayerB和PlayerC以前都是对手,但PlayerB是第一个对手,因为上一场比赛比PlayerC最近 PlayerE和PlayerD是对手的对手,PlayerE是之
Player <- HAS_PLAYERS - Game
Node Player: playerId, name,...etc
Node Game: gameId, gameDate
Rel. HAS_PLAYERS: result
说明:
PlayerB
和PlayerC
以前都是对手,但PlayerB
是第一个对手,因为上一场比赛比PlayerC
最近
PlayerE
和PlayerD
是对手的对手,PlayerE
是之前的对手,因为下一场比赛将在12月举行
我有下一个查询,但我的问题是该查询返回重复的节点:
任何帮助都将不胜感激。当您聚合节点时,它不会删除重复项,因此添加关键字“distinct”将修复它。代替收集(o),使用收集(不同的o)作为对手,收集(不同的行动)
//获得直接对手
匹配(p:Player{userId:“34618”})(o:Player)
按g.gameDate DESC的p、o、g顺序
使用p,收集(不同的o)作为对手
//获取对手的对手(ops)
比赛(p)-[:有玩家*3]-(共和党人:游戏)-(共和党人:玩家)
其中p.userId ops.userId,而不是ops
//尝试删除重复的节点
有不同的行动、对手、共和党
与对手,行动,gops按gops排序。游戏日期描述
//Concat两个列表:对手和对手的对手
使用REDUCE(s=对手,收集中的o2(不同的ops)| s+o2)作为列表所有对手
将ListAlloponents作为对立面展开
返回层
结果:
PlayerB
玩家
普拉耶尔
玩家
这是我的解决方案:
# Getting direct opponents
MATCH (p:Player {userId: "PlayerA"})<-[:HAS_PLAYERS]-(g:Game)-[:HAS_PLAYERS]->(o:Player)
WITH p, o, max(g.gameDate) as maxDate
WITH p, o ORDER BY maxDate DESC
WITH p, COLLECT(o) AS opponents
# Getting opponents-of-opponents (ops)
OPTIONAL MATCH (p)-[:HAS_PLAYERS*3]-(gops:Game)--(ops:Player)
WHERE p.userId <> ops.userId AND NOT ops IN opponents
WITH opponents, ops, max(gops.start) as maxDate
WITH opponents, ops ORDER BY maxDate DESC
WITH opponents, COLLECT(ops) AS opponentsOfOpponents
# Concat both lists: opponents and opponents-of-opponents
UNWIND (opponents + opponentsOfOpponents) AS player
RETURN player
#获得直接对手
比赛(p:Player{userId:“PlayerA”})(o:Player)
以p,o,max(g.gameDate)作为maxDate
按maxDate DESC的p、o订单
使用p,收集(o)作为对手
#获取对手的对手(ops)
可选比赛(p)-[:有_玩家*3]-(gops:游戏)-(ops:玩家)
其中p.userId ops.userId,而不是ops
与对手,行动,最大(gops.start)作为maxDate
对于对手,行动命令由maxDate描述
对于对手,收集(ops)作为对手的对手
#Concat两个列表:对手和对手的对手
作为球员放松(对手+对手中的对手)
还击球员
playerA如何与玩家B和玩家C进行相同的游戏?你是说同一个比赛日期而不是同一场比赛?这个玩家ID是谁:34618?游戏有1到4名玩家。这是一个离线在线的系统,一些玩家没有被添加到数据库中。ID为34618的玩家是PlayerA。我更喜欢用代号而不是id来简化问题。非常感谢。你给了我一个很好的线索,我可以自己解决。
# Getting direct opponents
MATCH (p:Player {userId: "PlayerA"})<-[:HAS_PLAYERS]-(g:Game)-[:HAS_PLAYERS]->(o:Player)
WITH p, o, g ORDER BY g.gameDate DESC
WITH p, COLLECT(o) AS opponents
# Getting opponents-of-opponents (ops)
MATCH (p)-[:HAS_PLAYERS*3]-(gops:Game)--(ops:Player)
WHERE p.userId <> ops.userId AND NOT ops IN opponents
# Trying to remove duplicated nodes
WITH DISTINCT ops, opponents, gops
WITH opponents, ops, gops ORDER BY gops.gameDate DESC
# Concat both lists: opponents and opponents-of-opponents
WITH REDUCE(s = opponents, o2 IN COLLECT(ops) | s + o2) as listAllOpponents
UNWIND listAllOpponents as opPlayer
RETURN opPlayer
PlayerB
PlayerC
PlayerD
PlayerE
PlayerD
// Getting direct opponents
MATCH (p:Player {userId: "34618"})<-[:HAS_PLAYERS]-(g:Game)-[:HAS_PLAYERS]->(o:Player)
WITH p, o, g ORDER BY g.gameDate DESC
WITH p, COLLECT(DISTINCT o) AS opponents
// Getting opponents-of-opponents (ops)
MATCH (p)-[:HAS_PLAYERS*3]-(gops:Game)--(ops:Player)
WHERE p.userId <> ops.userId AND NOT ops IN opponents
// Trying to remove duplicated nodes
WITH DISTINCT ops, opponents, gops
WITH opponents, ops, gops ORDER BY gops.gameDate DESC
// Concat both lists: opponents and opponents-of-opponents
WITH REDUCE(s = opponents, o2 IN COLLECT(DISTINCT ops) | s + o2) as listAllOpponents
UNWIND listAllOpponents as opPlayer
RETURN opPlayer
Result:
PlayerB
PlayerC
PlayerE
PlayerD
# Getting direct opponents
MATCH (p:Player {userId: "PlayerA"})<-[:HAS_PLAYERS]-(g:Game)-[:HAS_PLAYERS]->(o:Player)
WITH p, o, max(g.gameDate) as maxDate
WITH p, o ORDER BY maxDate DESC
WITH p, COLLECT(o) AS opponents
# Getting opponents-of-opponents (ops)
OPTIONAL MATCH (p)-[:HAS_PLAYERS*3]-(gops:Game)--(ops:Player)
WHERE p.userId <> ops.userId AND NOT ops IN opponents
WITH opponents, ops, max(gops.start) as maxDate
WITH opponents, ops ORDER BY maxDate DESC
WITH opponents, COLLECT(ops) AS opponentsOfOpponents
# Concat both lists: opponents and opponents-of-opponents
UNWIND (opponents + opponentsOfOpponents) AS player
RETURN player