cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Create relationship, if another relationship doesnt exist

Hi
I have a relationship between a vessel and a product (vessel)-[:HasInventory]->(product) when I forecast what a vessel may need to complete the voyage I get other products back plus products that the vessel may own, I don't want to add a forecasted relationship between any product the vessel where it has a [:HasInventory] relationship, can I do this completely in cypher or is it better I just take the nodes back iterate and add the [:Forecasted] as I go?

regards

1 ACCEPTED SOLUTION

ameyasoft
Graph Maven

Hi,

Existing relationship:
2X_6_61ec92c73a943edffb173f2ba07bbf668d35eb2c.png

Add 'ForeCasted' relationship to the vessel node. Here is the sample that I created:

MERGE(v:Vessel {name:"V1"})
MERGE(p:Product {name:"P1"})
MERGE(p1:Product {name:"P2"})
MERGE (v)-[:HasInventory]->(p)
MERGE (v)-[:ForeCasted]->(p1);

Result:
2X_3_378e8a797ec6691093a015a05f2f5f91681e0d78.png

MATCH (v)-[:HasInventory]->(p)
OPTIONAL MATCH (v)-[:ForeCasted]->(p1)
RETURN v.name as Vessel, p.name as HasInventory, p1.name as Forecasted ;

Result:
2X_3_378a5e4a98390eeda9b3333e1e9f85ae99eb52e7.png

Add this 'ForeCasted' relationship only if the vessel has any forecast products.

-Kamal

View solution in original post

2 REPLIES 2

ameyasoft
Graph Maven

Hi,

Existing relationship:
2X_6_61ec92c73a943edffb173f2ba07bbf668d35eb2c.png

Add 'ForeCasted' relationship to the vessel node. Here is the sample that I created:

MERGE(v:Vessel {name:"V1"})
MERGE(p:Product {name:"P1"})
MERGE(p1:Product {name:"P2"})
MERGE (v)-[:HasInventory]->(p)
MERGE (v)-[:ForeCasted]->(p1);

Result:
2X_3_378e8a797ec6691093a015a05f2f5f91681e0d78.png

MATCH (v)-[:HasInventory]->(p)
OPTIONAL MATCH (v)-[:ForeCasted]->(p1)
RETURN v.name as Vessel, p.name as HasInventory, p1.name as Forecasted ;

Result:
2X_3_378a5e4a98390eeda9b3333e1e9f85ae99eb52e7.png

Add this 'ForeCasted' relationship only if the vessel has any forecast products.

-Kamal

Thanks, I ended up omitting the hasholding items from the spatial intersect and just applying the appropriate relationship to the results

WITH "LINESTRING(6.1709 42.8694,5.90065916861194 43.0423602080088)" as route CALL spatial.intersects('productsgeom', route) YIELD node WHERE NOT (:Vessel{mmsi:248291000})-[:HasHolding]-(node) return node