How to mimic conditions while running a query to create relationships

I'm using a neo4. 4.3.3 community server on an Ubuntu 20.04.3 machine

I have the following problem: I have a set of X and a set of Y object.
So I start with

MATCH (x:X)
MATCH (y:Y)

Now, I need to create a relationship between each X object and Y object (an n*m cartesian product), with some attributes on the relationships, something like the following:

MERGE (x)-[r:IS_REL]->(y)
     SET r.a = 0 if ((x.a > 0 AND y.a = 0) OR (x.a = 0 AND y.a > 0))
             r.a = (abs(x.a - y.a) * 1.5) else
             
             r.b = 0 if ((x.b > 0 AND y.b = 0) OR (x.b = 0 AND y.b > 0))
             r.b = (abs(x.b - y.b) * 1.0) else

I spent a lot of time trying a lot of different approaches, with no success.
I also tried to implement it using apoc.case, but failing.

Can someone help?

Thank you.

Paolo

Try this:
MERGE (x)-[r:IS_REL]->(y)
with x, r, y

FOREACH(ignoreMe IN CASE WHEN ((x.a > 0 AND y.a = 0) OR (x.a = 0 AND y.a > 0)) and r.a = (abs(x.a - y.a) * 1.5)  THEN [1] ELSE [] END|

	set r.a = 0
	
)	

FOREACH(ignoreMe IN CASE WHEN ((x.b > 0 AND y.b = 0) OR (x.b = 0 AND y.b > 0)) and r.b = (abs(x.b - y.b) * 1.0)  THEN [1] ELSE [] END|

	set r.b = 0
	
)

Hello there,
I understand you want to do -

if ((x.a > 0 AND y.a = 0) OR (x.a = 0 AND y.a > 0)) then r.a = 0
else r.a = (abs(x.a - y.a) * 1.5)

if ((x.b > 0 AND y.b = 0) OR (x.b = 0 AND y.b > 0)) then r.b = 0
else r.b = (abs(x.b - y.b) * 1.0)

Is this right?

Can you try this -

MATCH (x:X)

MATCH (y:Y)

MERGE (x)-[r:IS_REL]->(y)

     SET r.a = (case when (x.a > 0 AND y.a = 0) OR (x.a = 0 AND y.a > 0) then 0 

         else abs(x.a - y.a) * 1.5 end),

         r.b = (case when (x.b > 0 AND y.b = 0) OR (x.b = 0 AND y.b > 0) then 0 

         else abs(x.b - y.b) * 1.0 end)

Here are the nodes I created to test this -

CREATE (x1:X {a:0,b:0})
CREATE (x2:X {a:0,b:2})
CREATE (x3:X {a:2,b:0})
CREATE (x4:X {a:2,b:2})

CREATE (y1:Y {a:0,b:0})
CREATE (y2:Y {a:0,b:4})
CREATE (y3:Y {a:4,b:0})
CREATE (y4:Y {a:4,b:4})

1 Like