对于我的生活,我似乎无法为以下场景构建查询。
给定与配料节点有关系的配方节点。 找到另一种具有相同成分的食谱。
非常感谢任何正确方向的指针。
这当然不起作用,但是尝试失败的一个例子
match (r:Recipie {name:"sweetcake"})-[:HAS_INGREDIENT]-(i:Ingredient)
with collect(distinct i.name) as Ingredients
match (r2:Recipie)-[:HAS_INGREDIENT]-(i2:Ingredient)
where (Ingredients IN colect(distinct i2.name))
return r2 limit 5
将其切换为
with collect(distinct i) as Ingredients
....
where (i2 IN Ingredients )
...
接近但是我得到的配方有原始甜饼节点的一种成分。
答案 0 :(得分:4)
我已经整理了一个示例数据集来展示这一点,以确保我理解您的问题。我相信你想要得到至少所有相同成分的配方,这意味着其他配方必须具有与Sweet Cake配方相同的成分,但也可能含有更多成分。
CREATE (sweetcake:Recipe {name:'Sweet Cake'}),
(chocolatecake:Recipe {name:'Chocolate Cake'}),
(lemoncake:Recipe {name:'Lemon Cake'}),
(sugar:Ingredient {name:'Sugar'}),
(something:Ingredient {name:'Something Nice'}),
(milk:Ingredient {name:'Milk'}),
(chocolate:Ingredient {name:'Chocolate'}),
(lemon:Ingredient {name:'Lemon'}),
(sweetcake)-[:HAS_INGREDIENT]->(sugar),
(sweetcake)-[:HAS_INGREDIENT]->(something),
(sweetcake)-[:HAS_INGREDIENT]->(milk),
(chocolatecake)-[:HAS_INGREDIENT]->(sugar),
(chocolatecake)-[:HAS_INGREDIENT]->(something),
(chocolatecake)-[:HAS_INGREDIENT]->(milk),
(chocolatecake)-[:HAS_INGREDIENT]->(chocolate),
(lemoncake)-[:HAS_INGREDIENT]->(sugar),
(lemoncake)-[:HAS_INGREDIENT]->(milk),
(lemoncake)-[:HAS_INGREDIENT]->(lemon)
我很确定你想要归还巧克力蛋糕,因为它分享了所有的Sugar,Something Nice和Milk with Sweet Cake。柠檬蛋糕不应该归还,因为它没有好吃的东西。因此,使用ALL()
检查配方是否至少含有与Sweet Cake食谱相同的成分。
MATCH (:Recipe {name:'Sweet Cake'})-[:HAS_INGREDIENT]->(i:Ingredient)
WITH COLLECT(i) AS sweetcake_ingredients
MATCH (r:Recipe)-[:HAS_INGREDIENT]->(i:Ingredient)
WHERE r.name <> 'Sweet Cake'
WITH sweetcake_ingredients, r, COLLECT(i) AS other_ingredients
WHERE ALL(x IN sweetcake_ingredients WHERE x IN other_ingredients)
RETURN r.name
只返回巧克力蛋糕。