请求帮助通过R-neo4j查询Cypher查询

时间:2015-05-18 10:38:20

标签: r neo4j cypher r-neo4j

我刚开始用R,neo4j和R-neo4j编程,所以如果我的问题很简单,请放纵。

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

数据库包含四名玩家之间的电脑游戏匹配结果。数据集由四个节点组成,即播放器1到播放器4.节点通过关系"失败"连接,表示匹配的结果。每个关系附有两个标签条目,包含以下数据:判断,游戏。

使用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")

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

看看你的图表,有点让我觉得没有合适的结构。尽管每种情况可能都不同,但考虑添加更多数据时会发生什么情况总是好的。你的模型可以处理吗?

例如,您使用关系来表示游戏结果,然后当然需要属性来存储裁判和游戏。游戏名称实际上看起来像锦标赛游戏,但你会知道哪些更好。当存储玩家和锦标赛名称时,你最终会有很多重复,因为到处都会出现相同的名字和玩家。

如果你继续在玩家之间添加结果,你最终会有很多关系,错误和重复的可能性会不断增加。

为了改善您的模型,您可以做些什么?把你的基本关系想象为一个起点,但现在已经超出了原来的要求:你可以为比赛和节点引入节点;保持关系以存储玩家在游戏中的角色等等。总有不止一种方法(TIMTOWTDI)。

鉴于图片胜过千言万语,请在此处查看改进的模型:

Improved Graph Model 您将看到如何更容易地将其他属性添加到模型中的相应节点或关系。

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

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'

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