我有一个唯一出现[:IN] a(:City)节点的非唯一节点(:Neighborhood)。我想创建一个新的邻域节点,并且只有在该城市中不存在该邻域节点时才建立它的关系。可以有多个具有相同名称的社区,但每个社区必须出现在房产城市中。
根据吉尔答案的建议:Return node if relationship is not present,我该怎么做:
MATCH a WHERE NOT (a:Neighborhood {name : line.Neighborhood})-[r:IN]->(c:City {name : line.City})
ON MATCH SET (a)-[r]-(c)
那么只有在城市中不存在的情况下,它才会创建一个新的邻域节点。
**更新:**我升级并对其进行了分析,仍然无法利用任何优化......
PROFILE LOAD CSV WITH HEADERS FROM "file://THEFILE" as line
WITH line LIMIT 0
MATCH (c:City { name : line.City})
MERGE (n:Neighborhood {name : toInt(line.Neighborhood)})-[:IN]->(c)
;
+--------------+------+--------+---------------------------+------------------------------+
| Operator | Rows | DbHits | Identifiers | Other |
+--------------+------+--------+---------------------------+------------------------------+
| EmptyResult | 0 | 0 | | |
| UpdateGraph | 5 | 16 | anon[340], b, neighborhood, line | MergePattern |
| SchemaIndex | 5 | 10 | b, line | line.City; :City(name) |
| ColumnFilter | 5 | 0 | line | keep columns line |
| Filter | 5 | 0 | anon[216], line | anon[216] |
| Extract | 5 | 0 | anon[216], line | anon[216] |
| Slice | 5 | 0 | line | { AUTOINT0} |
| LoadCSV | 5 | 0 | line | |
+--------------+------+--------+---------------------------+------------------------------+
答案 0 :(得分:2)
我认为你可以简单地使用MERGE
:
MATCH (c:City {name: line.City})
MERGE c<-[:IN]-(a:Neighborhood {name : line.Neighborhood})
如果您尚未导入所有城市,则可以使用MERGE
创建这些城市:
MATCH (c:City {name: line.City})
MERGE c<-[:IN]-(a:Neighborhood {name : line.Neighborhood})
但要注意Eager
运算符:
http://www.markhneedham.com/blog/2014/10/23/neo4j-cypher-avoiding-the-eager/
简而言之:您应该运行LOAD CSV
(我假设您在这里做的事情)两次,一次加载城市,一次加载社区。 p>