Tom Rijntjes
Tom Rijntjes

Reputation: 654

conditionally changing parameter inside ON MATCH SET clause

I'm implementing a learning algorithm where the relationship between two nodes is strengthened when they are used together. The cost attribute should be limited to 1, so I want to expand the following query

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 *

to something like this, with my goal added in pseudocode

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 *

How to translate the if-clause to Cypher?

Upvotes: 1

Views: 62

Answers (1)

cybersam
cybersam

Reputation: 67044

This should work:

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 *;

Upvotes: 2

Related Questions