Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
02-27-2021 11:44 PM
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);
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
Solved! Go to Solution.
02-28-2021 12:28 AM
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
02-28-2021 12:28 AM
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
02-28-2021 12:46 AM
Perrrfect 🙂 Thanks
I think I didn't understand what OPTIONAL MATCH does when I was reading about it. Will go back to learn it
All the sessions of the conference are now available online