neo4j MERGE,SET,CASE

时间:2016-09-24 06:18:24

标签: neo4j cypher

我试图做一个棘手的查询。 我需要检查是否存在关系;

  • 如果是的话我需要在该关系上设置属性(prop)为max(0,prop + val)
  • 如果不是我需要创建它将属性设置为max(0,value)

该财产可能会消极。

这基本上是我想要的,虽然这种格式不会起作用,因为你在MERGE之后不能有一个WITH语句:

MATCH (n1:Type1 {id:{id1}), (n2:Type2 {id: {id2}) 
MERGE (n1)-[r:RELATIONSHIP]->(n2) WITH CASE 
WHEN r.prop + {val} < 0 THEN 0 
ELSE r.prop + {val} END as prop 
ON CREATE SET r.prop = prop
ON MATCH SET r.prop = prop

这就是它的两个部分,尽管我不想再查询两次:

MATCH (n1:Type1 {id:{id1}), (n2:Type2 {id: {id2}) 
MERGE (n1)-[r:RELATIONSHIP]->(n2)
ON CREATE SET r.prop = 0

MATCH (n1:Type1 {id:{id1})-[r:RELATIONSHIP]->n2:Type2 {id: {id2})
WITH r, CASE 
WHEN r.prop + {val} < 0 THEN 0 
ELSE r.prop + {val} END as prop
SET r.prop = prop

2 个答案:

答案 0 :(得分:0)

我认为这个查询应该可以解决问题:

MATCH (n1:Type1 {id:{id1}), (n2:Type2 {id: {id2}) 
MERGE (n1)-[r:RELATIONSHIP]->(n2)
ON CREATE SET r.prop = 0
ON MATCH SET r.prop = 
CASE r.prop 
   WHEN r.prop + {val} < 0 
   THEN 0 
   ELSE r.prop + {val} 
   END

答案 1 :(得分:0)

MATCH (n1:Type1 {id:{id1}), (n2:Type2 {id: {id2}) 
MERGE (n1)-[r:RELATIONSHIP]->(n2)
ON CREATE SET r.prop = 
CASE
    WHEN {val} < 0 THEN 0
    ELSE {val}
    END
ON MATCH SET r.prop = 
CASE 
   WHEN r.prop + {val} < 0 
   THEN 0 
   ELSE r.prop + {val} 
   END