关于两个元素的 Neo4j 查询 [英] Neo4j query concerning two elements
问题描述
我需要帮助尝试在 Neo4j 中执行我似乎无法弄清楚的查询.查询是返回包含两种成分的所有蛋糕:牛奶和奶油.
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 似乎无法识别成分,但我并不完全确定我的查询格式是否正确,无论如何,这是我目前所拥有的:
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
推荐答案
这是一种从列表中获取包含所有成分的蛋糕的方法:
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
这篇关于关于两个元素的 Neo4j 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!