Create relationship, if another relationship doesnt exist

(Dirtgambit) #1

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?


(Ameyasoft) #2


Existing relationship:

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


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


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


(Dirtgambit) #3

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