How to create a relation between two existing nodes only when the relation has some value

How to create a relation between two existing nodes only when the relation has some value. Eg., I have node labels Juice, Sour, sweet, spicy, salty. I will connect Sweetlime juice node to Sour node with relation[:HAS {value:30%}] and to sweet node with relation[:HAS {value:60%}] and to salty node with relation[:HAS {value:10%}] . Similarly I want to connect banana juice to Sweet node with relation[:HAS {value:100%}].

I have used below query
LOAD CSV WITH HEADERS FROM "file:///juice.csv" as line
MERGE (j:Juice {name:line.Juice})
WITH j,line
MATCH (a:Taste_Type {name:"sour"}),(b:Taste_Type {name:"salt"}),(c:Taste_Type {name:"sweet"}),(d:Taste_Type {name:"spicy"})
MERGE (j)-[r:HAS]->(a) SET r.value=line.Sour
MERGE (j)-[s:HAS]->(b) SET s.value=line.Salt
MERGE (j)-[t:HAS]->(c) SET t.value=line.Sweet
MERGE (j)-[u:HAS]->(d) SET u.value=line.Spicy

When I use the above query it is creating relationship between juice node and each and every taste_type node though the relation has no value in csv. I dont want sweet lime to connect to spicy node and banana juice connect to salty,sour,spicy. Could you help me how can I write the query to achieve this.

Thanks in Advance.

Hi!

Could you please share with me what the first few lines of your CSV look like, including header?

Thanks!

juice

Give it a try:

LOAD CSV WITH HEADERS FROM "file:///juice.csv" as line
MERGE (j:Juice {name:line.Juice})
WITH j,line
MATCH (a:Taste_Type {name:"sour"}),(b:Taste_Type {name:"salt"}),(c:Taste_Type {name:"sweet"}),(d:Taste_Type {name:"spicy"})
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Sour) && line.Sour IS NOT NULL THEN [1] ELSE [] END |
    MERGE (j)-[r:HAS]->(a) SET r.value=line.Sour
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Salt) && line.Salt IS NOT NULL THEN [1] ELSE [] END | 
    MERGE (j)-[r:HAS]->(b) SET r.value=line.Salt
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Sweet) && line.Sweet IS NOT NULL THEN [1] ELSE [] END | 
    MERGE (j)-[r:HAS]->(c) SET r.value=line.Sweet
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Spicy) && line.Spicy IS NOT NULL THEN [1] ELSE [] END | 
    MERGE (j)-[r:HAS]->(d) SET r.value=line.Spicy
);

Thank you for the response. I tried above query. But it didnt work. It created nodes but not any of the relations.

change this condition for each foreach to appropriate one and try again

I have used the below query when I tried.

LOAD CSV WITH HEADERS FROM "file:///juice.csv" as line
MERGE (j:Juice {name:line.Juice})
WITH j,line
MATCH (a:Taste_Type {name:"sour"}),(b:Taste_Type {name:"salt"}),(c:Taste_Type {name:"sweet"}),(d:Taste_Type {name:"spicy"})
FOREACH(ignoreMe IN CASE WHEN line.Sour IS NOT NULL THEN [1] ELSE END |
MERGE (j)-[r:HAS]->(a) SET r.value=line.Sour
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Salt) and line.Salt IS NOT NULL THEN [1] ELSE END |
MERGE (j)-[s:HAS]->(b) SET s.value=line.Salt
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Sweet) and line.Sweet IS NOT NULL THEN [1] ELSE END |
MERGE (j)-[t:HAS]->(c) SET t.value=line.Sweet
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Spicy) and line.Spicy IS NOT NULL THEN [1] ELSE END |
MERGE (j)-[u:HAS]->(d) SET u.value=line.Spicy
)

Is it correct?

Check this one

LOAD CSV WITH HEADERS FROM "file:///juice.csv" as line
MERGE (j:Juice {name:line.Juice})
WITH j,line
MATCH (a:Taste_Type {name:"sour"}),(b:Taste_Type {name:"salt"}),(c:Taste_Type {name:"sweet"}),(d:Taste_Type {name:"spicy"})
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Sour)  THEN [1] ELSE [] END |
    MERGE (j)-[r:HAS]->(a) SET r.value=line.Sour
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Salt)  THEN [1] ELSE [] END | 
    MERGE (j)-[r:HAS]->(b) SET r.value=line.Salt
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Sweet)  THEN [1] ELSE [] END | 
    MERGE (j)-[r:HAS]->(c) SET r.value=line.Sweet
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Spicy) THEN [1] ELSE [] END | 
    MERGE (j)-[r:HAS]->(d) SET r.value=line.Spicy
);

It didn't work too. I tried by removing EXISTS() and having only NOT NULL condition in FOREACH also. But it didn't work too.

Did you find any solution for this?

Can share the CSV file

I shared the csv in above conversation. I cannot upload csv file as it allows only png, jpeg formats. Hence I attached screenshot of csv in above conversation.

hi,
I have tested the code

Here what I have used

I have created four nodes

CREATE (:Taste_Type {name:"sour"});
CREATE (:Taste_Type {name:"salt"});
CREATE (:Taste_Type {name:"sweet"});
CREATE (:Taste_Type {name:"spicy"});

link to csv: juice.csv

Now I have created the relations and juice using the following query

LOAD CSV WITH HEADERS FROM "file:///juice.csv" as line
MERGE (j:Juice {name:line.Juice})
WITH j,line
MATCH (a:Taste_Type {name:"sour"}),(b:Taste_Type {name:"salt"}),(c:Taste_Type {name:"sweet"}),(d:Taste_Type {name:"spicy"})
WITH j, line, a, b, c, d
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Sour)  THEN [1] ELSE [] END |
    MERGE (j)-[r:HAS]->(a) SET r.value=line.Sour
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Salt)  THEN [1] ELSE [] END | 
    MERGE (j)-[r:HAS]->(b) SET r.value=line.Salt
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Sweet)  THEN [1] ELSE [] END | 
    MERGE (j)-[r:HAS]->(c) SET r.value=line.Sweet
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Spicy) THEN [1] ELSE [] END | 
    MERGE (j)-[r:HAS]->(d) SET r.value=line.Spicy
);

Taste_Type nodes: Taste_Type nodes

relations: relations

Here a better version of the relation query:

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///juice.csv" as line
MERGE (j:Juice {name:line.Juice})
WITH j,line
MATCH (a:Taste_Type {name:"sour"})
MATCH (b:Taste_Type {name:"salt"})
MATCH (c:Taste_Type {name:"sweet"})
MATCH (d:Taste_Type {name:"spicy"})
WITH j, line, a, b, c, d
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Sour)  THEN [1] ELSE [] END |
    MERGE (j)-[r:HAS]->(a) SET r.value=line.Sour
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Salt)  THEN [1] ELSE [] END | 
    MERGE (j)-[r:HAS]->(b) SET r.value=line.Salt
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Sweet)  THEN [1] ELSE [] END | 
    MERGE (j)-[r:HAS]->(c) SET r.value=line.Sweet
)
FOREACH(ignoreMe IN CASE WHEN EXISTS(line.Spicy) THEN [1] ELSE [] END | 
    MERGE (j)-[r:HAS]->(d) SET r.value=line.Spicy
)

Let me know if this is what you needed.