Create relationship, if another relationship doesnt exist


(Dirtgambit) #1

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


(Ameyasoft) #2

Hi,

Existing relationship:
vessel1

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:
vessel3

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

Result:
vessel2

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

-Kamal


(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