在ON MATCH SET子句中有条件地改变参数

时间:2015-09-15 14:26:16

标签: neo4j cypher

我正在实施一种学习算法,当两个节点一起使用时,两个节点之间的关系会得到加强。 cost属性应限制为1,因此我想扩展以下查询

MATCH (a:Node),(b:Node)
WHERE a.tag = "tag1" AND b.tag = "tag2"
MERGE (a)-[r:SIMILAR]->(b)
ON CREATE SET r.cost = 0.01*exp(4.60517*0.5)
ON CREATE SET r.costx = 0.5
ON MATCH SET r.cost = 0.01*exp(4.60517*(r.costx-0.01))
ON MATCH SET r.costx = r.costx -0.01 
RETURN *

这样的事情,我的目标是在伪代码中加入

MATCH (a:Node),(b:Node)
WHERE a.tag = "tag1" AND b.tag = "tag2"
MERGE (a)-[r:SIMILAR]->(b)
ON CREATE SET r.cost = 0.01*exp(4.60517*0.5)
ON CREATE SET r.costx = 0.5
if r.cost < 1
    ON MATCH SET r.cost = 0.01*exp(4.60517*(r.costx-0.01))
    ON MATCH SET r.costx = r.costx -0.01 
RETURN *

如何将if子句翻译成Cypher?

1 个答案:

答案 0 :(得分:2)

这应该有效:

MATCH (a:Node),(b:Node)
WHERE a.tag = "tag1" AND b.tag = "tag2"
MERGE (a)-[r:SIMILAR]->(b)
ON CREATE SET r.cost = 0.01*exp(4.60517*0.5)
ON CREATE SET r.costx = 0.5
ON MATCH SET r.cost =
  CASE WHEN r.cost < 1 THEN 0.01*exp(4.60517*(r.costx-0.01)) ELSE r.cost END
ON MATCH SET r.costx =
  CASE WHEN r.cost < 1 THEN r.costx - 0.01 ELSE r.costx END
RETURN *;