Struggling with Cypher & CREATE UNIQUE

痴心易碎 提交于 2019-12-11 08:39:23

问题


I am reading soccer match data from text file and want to create matches and referee nodes. The way I want logic to work is that I create a match node and then I get the referee name and create the referee node ONLY if that referee does not already exist else I just link the existing referee to the match. I presently DO NOT have a root node and am not sure if I should create one (very new to graph modeling).

I have the following query in which I think I am close but not there.

$match= $client->makeNode();
$match->setProperty('label', "match: ".$feed['match_number'])
      ->setProperty('type', "match")->save();

//now that the match node is created lets see if the current referee in feed exists already

$queryString = "START match=node({nodeId}) ".   <----- NEED TO LOOK AT ALL CASES?????
"CREATE UNIQUE (referee{label:{name}, type:'referee'})-[:REFEREED{ label:'REFEREED' }]->(match)"."RETURN referee";

$query = new Neo4j\Cypher\Query($client, $queryString, array('nodeId' => $match->getId(),'name' => $feed['referee_name']));
$result = $query->getResultSet();

Can someone please assist?


回答1:


One of the possible models that at the moment seem to satisfy the queries you've posted:

(Team)-[:PLAYS]->(Match)

(Match)-[:HAS_REFEREE]->(Referee)

(Match)-[:PLAYED_IN]->(City)

The PLAYS relation could have a property to indicate if the team was the home team. You could also have a property on the PLAYS relation to indicate whether that team won or not. Or if winning is a big part of what you're looking for, you can create an extra relation such as (Team)-[:WON]->(Match) (though then you need to think about how to model draws. The absence of a WON relation on either of the two teams for a match could indicate a draw maybe).

  1. All matches for a particular referee: Start at the referee, traverse through the Match to the Cities. You might index some unique property of the referee to be able to look him up quickly

  2. All matches where the referee worked and the home team won: Start at the referee, find all his matches, filter on the WON relation/property and the home team property

  3. All referees that have the highest count of wins for the home team: Same as above, start at all referees

  4. Most active referees for a city: Start at the city, find all matches and their referees

You might move things around a bit depending on more questions that you want to answer (especially home team properties, WIN/LOSE relations or properties etc.)

And I don't think you need the root node at all. You can index all matches/cities/referees etc if you want to find all of them



来源:https://stackoverflow.com/questions/17506754/struggling-with-cypher-create-unique

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!