rneo4jcypherr-neo4j

Request for help wirting Cypher query through R-neo4j


I just started programming in R, neo4j and R-neo4j so please be indulgent if my question is trivial.

I have created the following R Project code [2].

The database contains the outcome of computer game matches between four players. The dataset consists of four nodes, player 1 to player 4. The nodes are connected via the relationship "defeats", which indicates the outcome of the matches. There are two label entries attached to each relationship containing the following data: judge, game.

From the graph database using Cypher queries, I want to extract data in the following form (please confer the picture in [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

I want to make a query (preferred in the R-neo4j environment) to the graph database, where the input is "player 1" and the table above is returned.

I hope that my question is clear and someone can help me with this.

[2] The R (Rneo4j) code:

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

Solution

  • A couple things. If you want to use clear(graph) without having to type "Y", you can use clear(graph, input=F). Also, if you weren't aware, you can set properties on relationships when you create them:

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

    To answer the question, I'd do this:

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

    Output:

      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