Cypher query to match multiple Destination nodes

Hi there,

I have a requirement where i want to include multiple destination in my path.

My Original Graph

My requirement:

I want to find all the suppliers and the RISK associated with the Suppliers for a particular product.

Query to return all suppliers for a Product.

match path =(p:Product{name:"Product2"}) <-[*..10] -(:Supplier)
return path

This query returns me all the suppliers for a particular product

Query to return suppliers affected by RISK for a product

match path =(p:Product{name:"Product2"}) <-[*..10] -(:Supplier)-[:AFFECTEDBY]-(:RISK)
return path

As you can see the 2 suppliers (name:"SupplierN+1") were not retrieved in the above graph.

So basically I need a combination of both the queries.

Can you please help me with the query to retrieve BOTH Suppliers (3 suppliers) and RISK associated with for a particular product.

Thanks Heaps

Try this:

match (p:Product{name:"Product2"}) <-[*..10] -(s:Supplier)
optional match (s)-[:AFFECTEDBY]-(r:RISK)
return p, s, r

thanks for your reply ameyasoft.

I tried the above query, but the problem is i lose the links along the path. screenshot below.

How can retain the path while getting BOTH Suppliers (3 suppliers) and RISK

Try this:

match (a:Supplier)-[]-(b:Supplier) where b.name <> a.name
 with a, b
 match path = (a)-[]-(b)-[*..10]->(p:Product)
 where p.name = "Product2"
 return path

This doesn't help. I am not sure if you got my question.
How can retain the path while getting BOTH Suppliers and RISK.

Is there a way i can put 2 destination nodes i.e. (destination:Supplier | :RISK)

match path =(p:Product{name:"Product2"}) <-[*..10] -(destination:Supplier | :RISK)
return path

 match path = (a)-[]-(b:Supplier)-[*..10]->(p:Product)
 where p.name = "Product2"
 return path
1 Like

Is there a way i can put 2 destination nodes i.e. (destination:Supplier | :RISK)

Yes.

MATCH path =(p:Product{name:"Product2"}) <-[*..10] -(destination)
WHERE destination:Supplier OR destination:RISK
RETURN path

If you don't mind working with multiple paths, then you can use this instead, which builds on the OPTIONAL MATCH approach suggested earlier:

MATCH path =(p:Product{name:"Product2"}) <-[*..10] -(s:Supplier)
OPTIONAL MATCH riskPath = (s)-[:AFFECTEDBY]-(:RISK)
RETURN path, riskPath
1 Like

Works , Thanks ameyasoft :)

This is so helpful. Thanks a lot andrew :)