Return node with all nodes it's in relationship but some of the related nodes meet a condition

Hi,

I'm new to Neo4j so I might be doing it the wrong way!

I have a set of nodes called Recipe. Each Recipe is in a relationship with one or more Ingredient. Now I want to search for Recipes that contain a specific list of Ingredients.

For example, if my ingredient list in search criteria is "Asparagus", it must return "Scrambled Eggs with Asparagus" which contains Asparagus but also includes other ingredients that weren't in the search criteria.

I have this Cypher query and it returns the recipe correctly:

MATCH (n:Recipe)-[r:INGREDIENTS]->(m:Ingredient) where (m.name = 'Asparagus') return n,collect(r),collect(m);

image

But my problem is that it only returns the Recipe node and the Asparagus node while I want it to return Recipe node with all of its ingredients. I tried "exists" but it returns the same result but slower!

Does anyone know what I can do?

Any help is much appreciated :slight_smile:

mi stands for main Ingredients
si stands for second Ingredients

MATCH (mi:Ingredient)<-[:NEEDS]-(r:Recipe) WHERE mi.name IN ['Asparagus']
OPTIONAL MATCH (r)-[:NEEDS]->(si)
RETURN r AS recipe, collect(mi.name) + collect(si.name) AS ingredients

1 Like

Perrrfect :) Thanks

I think I didn't understand what OPTIONAL MATCH does when I was reading about it. Will go back to learn it :slight_smile: