Neo4J RelationShips MERGE ONCREATE ONMATCH

时间:2016-03-01 17:41:06

标签: neo4j cypher oncreate relationships

是否有关于MERGE关系的独特ON CREATE和ON MATCH的解决方案?

例如:

USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS FROM 'http://ip/myfile.csv' AS row
MATCH (source:Ticket {Id: toInt(row.idticket)})
MATCH (target:User {Id: toInt(row.iduser)})
MERGE (source)-[rel:`AskBy` {}]->(target);

做这样的事情

USING PERIODIC COMMIT 10000
LOAD CSV WITH HEADERS FROM 'http://ip/myfile.csv' AS row
MATCH (source:Ticket {Id: toInt(row.idticket)})
MATCH (target:User {Id: toInt(row.iduser)})
MATCH (source)-[rel:`AskBy` {}]->(target);
ON CREATE SET (source)-[rel:`AskBy` {createDate: timespan()}]->(target)
ON MATCH SET (source)-[rel:`AskBy` {updateDate: timespan()}]->(target)

我想区分关系的创建日期和更新日期

谢谢

2 个答案:

答案 0 :(得分:0)

您只需要设置属性:

MATCH (source:Ticket {Id: toInt(row.idticket)})
MATCH (target:User {Id: toInt(row.iduser)})
MERGE (source)-[rel:AskBy]->(target)
ON CREATE SET rel.createDate = timestamp()
ON MATCH SET rel.updateDate = timestamp();

答案 1 :(得分:0)

要回答@ NicoleWhite正确回答的评论中的后续问题:

  1. MERGE不允许任何条件。如果它不匹配,它将始终创建指定的模式。
  2. 以下是如何实现"条件MERGE"等效的示例。使用OPTIONAL MATCHFOREACH作为解决方法。

    这个例子假装这是所需的伪逻辑:

    • 如果AskBy关系不存在:

      • 如果(假设)source.priority值大于10,则创建关系(使用createDate属性)
      • 其他什么都不做
    • 否则设置其updateDate属性。

      USING PERIODIC COMMIT 10000
      LOAD CSV WITH HEADERS FROM 'http://ip/myfile.csv' AS row
      MATCH (source:Ticket {Id: toInt(row.idticket)})
      MATCH (target:User {Id: toInt(row.iduser)})
      OPTIONAL MATCH (source)-[rel:AskBy]->(target)
      WITH source, rel, target,
        (CASE
          WHEN rel IS NULL AND source.priority > 10 THEN {create: [1]}
          WHEN rel IS NULL THEN NULL
          ELSE {set: [1]}
        END) AS todo
      FOREACH (x IN todo.create | CREATE (source)-[rel:AskBy {createDate: timestamp()}]->(target))
      FOREACH (x IN todo.set | SET rel.updateDate = timestamp())