0

I need help trying to do a query in Neo4j that I can't seem to figure out. The query is to return all cakes that contain both the ingredients: Milk and Cream.

Below is a snippet of a cake node and the ingredients (There are more ingredients and cakes but I didn't post them here as they are all formatted the same):

  (brownies:Cake {name: "Brownies"}),
  (brownies)-[:CONTAINS {quantity: 50, unit: "grams"}]->(white),
  (brownies)-[:CONTAINS {quantity: 250, unit: "grams"}]->(selfraising),
  (brownies)-[:CONTAINS {quantity: .5, unit: "grams"}]->(salt),
  (brownies)-[:CONTAINS {quantity: 125, unit: "grams"}]->(sugar),
  (brownies)-[:CONTAINS {quantity: 250, unit: "grams"}]->(cocoa),
  (brownies)-[:CONTAINS {quantity: 125, unit: "grams"}]->(lemonade),
  (brownies)-[:CONTAINS {quantity: 125, unit: "grams"}]->(cola),
  (brownies)-[:GARNISHED_WITH {how: "chopped on top"}]->(cherry),
  (brownies)-[:GARNISHED_WITH {how: "chopped on top"}]->(orange),


  (limeJuice:Ingredient {name: "lime juice"}),
  (cranberryJuice:Ingredient {name: "cranberry juice"}),
  (lemonJuice:Ingredient {name: "lemon juice"}),
  (orangeJuice:Ingredient {name: "orange juice"}),
  (tomatoJuice:Ingredient {name: "tomato juice"}),
  (lemonade:Ingredient {name: "lemonade"}),
  (soda:Ingredient {name: "soda water"}),
  (spice:Ingredient {name: "spice water"}),
  (cola:Ingredient {name: "cola"}),

Neo4j seems to have trouble identifying ingredients but I'm not entirely sure that my query is formatted correctly regardless, here is what I have so far:

MATCH(x:Cake)-[:CONTAINS]-> (Ingredient: "milk" or "cream") Return x
1
  • Your (:Ingredient) node query is incorrect. Take a look at the WHERE ... IN... clause, which is where you can check for multiple values. May 27, 2020 at 17:45

2 Answers 2

0

Your Ingredient node check is problematic. Needs to be more like:

MATCH(x:Cake)-[:CONTAINS]-> (i:Ingredient)
WHERE i.name IN ['milk', 'cream']
Return x
0

Here is one way to get the cakes that contain ALL the ingredients from a list:

MATCH (cake:Cake)
WHERE ALL(x IN ['milk', 'cream'] WHERE (cake)-[:CONTAINS]->(:Ingredient{name: x}))
RETURN cake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.