0

我刚开始用 R、neo4j 和 R-neo4j 编程,所以如果我的问题是微不足道的,请多多包涵。

我使用 R-neo4j 和以下 R 项目代码 [2] 创建了以下数据库(请附上照片)[1]。

该数据库包含四个玩家之间的计算机游戏比赛的结果。数据集由四个节点组成,玩家 1 到玩家 4。这些节点通过关系“失败”连接,表示比赛的结果。每个关系都有两个标签条目,包含以下数据:judge、game。

从使用 Cypher 查询的图形数据库中,我想提取以下形式的数据(请在 [1] 中提供图片):

Winning player    Loosing player    Game         Judge
player 1          player 4          Starcraft    player 2
player 1          player 4          LOL          player 3
player 4          player 1          LOL          player 2
player 1          player 4          Starcraft    player 3
player 1          player 2          LOL          player 3
player 2          player 1          LOL          player 4
player 4          player 1          Starcraft    player 4

我想对图形数据库进行查询(在 R-neo4j 环境中首选),其中输入为“玩家 1”并返回上表。

我希望我的问题很清楚,有人可以帮助我。

祝你有美好的一天。

基督教

[1] https://goo.gl/cMxXHo

[2] R (Rneo4j) 代码:

clear(graph)
Y
player1 = createNode(graph,"user",ID="Player 1",male=T)
player2 = createNode(graph,"user",ID="Player 2",male=T)
player3 = createNode(graph,"user",ID="Player 3",male=F)
player4 = createNode(graph,"user",ID="Player 4",male=F)

addConstraint(graph,"user","ID")

rel1 = createRel(player1,"defeats",player4)
rel2 = createRel(player1,"defeats",player4)
rel3 = createRel(player4,"defeats",player1)
rel4 = createRel(player1,"defeats",player4)
rel5 = createRel(player1,"defeats",player2)
rel6 = createRel(player2,"defeats",player1)
rel7 = createRel(player3,"defeats",player1)

rel1 = updateProp(rel1, game = "Starcraft", judge = "Player 2")
rel2 = updateProp(rel2, game = "League of Legends", judge = "Player 3")
rel3 = updateProp(rel3, game = "League of Legends", judge = "Player 2")
rel4 = updateProp(rel4, game = "Starcraft", judge = "Player 3")
rel5 = updateProp(rel5, game = "League of Legends", judge = "Player 3")
rel6 = updateProp(rel6, game = "League of Legends", judge = "Player 4")
rel7 = updateProp(rel7, game = "Starcraft", judge = "Player 4")
4

2 回答 2

1

几件事。如果您想使用clear(graph)而不必键入“Y”,您可以使用clear(graph, input=F). 此外,如果您不知道,您可以在创建关系时设置它们的属性:

rel1 = createRel(player1, "defeats", player4, game="Starcraft", judge="Player 2")

要回答这个问题,我会这样做:

getDataForPlayer = function(name) {
  query = "
  MATCH (winner:user)-[game:defeats]->(loser:user)
  WHERE winner.ID = {name} OR loser.ID = {name}
  RETURN winner.ID AS `Winning Player`,
         loser.ID AS `Losing Player`,
         game.game AS Game,
         game.judge AS Judge
  "

  return(cypher(graph, query, name=name))
}

getDataForPlayer("Player 1")

输出:

  Winning Player Losing Player              Game    Judge
1       Player 4      Player 1 League of Legends Player 2
2       Player 2      Player 1 League of Legends Player 4
3       Player 3      Player 1         Starcraft Player 4
4       Player 1      Player 2 League of Legends Player 3
5       Player 1      Player 4         Starcraft Player 2
6       Player 1      Player 4 League of Legends Player 3
7       Player 1      Player 4         Starcraft Player 3
于 2015-05-18T11:44:56.053 回答
0

看着你的图表,我觉得没有正确的结构。尽管每种情况可能不同,但考虑添加更多数据时会发生什么总是好的。你的模型能应付吗?

例如,您正在使用关系来表示游戏的结果,这当然需要属性来存储裁判和游戏。游戏名称对我来说实际上看起来像锦标赛游戏,但你会知道什么更有效。当存储球员和锦标赛的名字时,你最终会有很多重复,因为相同的名字和球员出现在任何地方。

如果您继续在玩家之间添加结果,您最终会产生许多关系,并且错误和重复的可能性会不断增加。

那么你能做些什么来改进你的模型呢?将您的基本关系视为起点,但现在它已经超出了最初的要求:您可以为比赛引入节点,为游戏引入节点;保持关系以存储游戏中玩家的角色等。总是有不止一种方法可以做到这一点(TIMTOWTDI)。

鉴于一张图片值一千字,请看这里的改进模型:

改进的图模型 您会看到如何更轻松地向模型中的相应节点或关系添加其他属性。

为了生成带有结果的所需表格,您可以使用:

MATCH
  (g:Game)-[:WINNER]->(w:Player),
  (g)-[:LOSER]->(l:Player),
  (g)-[:JUDGE]->(j:Player),
  (g)<-[:HAS_GAMES]-(t:Tournament)
WHERE
  w.name = 'Player 1' OR l.name = 'Player 1'
RETURN
  w.name AS 'Winning Player',
  l.name AS 'Losing Player',
  t.name AS 'Game',
  j.name AS 'Judge'

并按照 Nicole 的建议适应 R。如果您假装添加大量数据,我认为这种结构会更好地适应您的需求,并且您还可以探索查询相同数据的不同方式,因为您现在可以从锦标赛开始或直接探索游戏。

于 2015-05-24T11:19:00.387 回答