Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
11-13-2019 04:14 AM
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.
11-13-2019 11:03 PM
Hi!
Could you please share with me what the first few lines of your CSV look like, including header?
Thanks!
11-14-2019 12:07 AM
11-14-2019 01:01 AM
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
);
11-18-2019 09:13 PM
Thank you for the response. I tried above query. But it didnt work. It created nodes but not any of the relations.
11-18-2019 09:18 PM
change this condition for each foreach to appropriate one and try again
11-18-2019 09:40 PM
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?
11-18-2019 11:22 PM
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
);
11-19-2019 01:23 AM
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.
11-25-2019 05:04 AM
Did you find any solution for this?
11-26-2019 09:04 PM
Can share the CSV file
11-26-2019 10:00 PM
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.
11-27-2019 09:37 PM
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.
All the sessions of the conference are now available online