如何优化和改进具有多重匹配和增加where子句数量的neo4j密码查询



在我的neo4j图形数据库中,我有一些餐厅链接到菜单上列出的食物,也链接到它们所在的城市。

我试图在伦敦寻找每一家都提供不同种类食物的餐厅,所以我开始用这种方式寻找两家不同的餐厅:

MATCH (f1:Food)--(r1:Restaurant)-[:LOCATED_IN]-(c1:City{name:'London'}) 
WHERE f1.name in [name1, name2, name3, name4] 
MATCH (f2:Food)--(r2:Restaurant)-[:LOCATED_IN]-(c2:City{name:'London'}) 
WHERE r1.id<>r2.id and f1.name<>f2.name 
RETURN, r1.name as Restaurant_A, collect(distinct f1.name) as Food_A,r2.name as Restaurant_B, collect(distinct f2.name) as Food_B 
LIMIT 1

但是,如果我想找第三家餐厅,我需要添加更多的where子句,等等。如果我想要第四家,第五家…

这是第三家餐厅的例子:

MATCH (f1:Food)--(r1:Restaurant)-[:LOCATED_IN]-(c1:City{name:'London'}) 
WHERE f1.name in [name1, name2, name3, name4] 
MATCH (f2:Food)--(r2:Restaurant)-[:LOCATED_IN]-(c2:City{name:'London'}) 
WHERE r1.id<>r2.id and f1.name<>f2.name  
MATCH (f3:Food)--(r3:Restaurant)-[:LOCATED_IN]-(c3:City{name:'London'}) 
WHERE r2.id<>r1.id and r3.id<>r2.id and f3.name<>f3.name  and f3.name<>f2.name  
RETURN r1.name as Restaurant_A, collect(distinct f1.name) as Food_A,r2.name as Restaurant_B, collect(distinct f2.name) as Food_B, r3.name as Restaurant_C, collect(distinct f3.name) as Food_C 
LIMIT 1

我真的很想知道是否有其他方法可以做到这一点,我是neo4j的新手,每一个建议都非常受欢迎。

假设您在一个数组中提供一个:Food节点的集合,则可以执行

MATCH (f:Food)--(r:Restaurant)-[:LOCATED_IN]-(c1:City{name:'London'}) 
WHERE f IN $foodNodes
RETURN r.name as restaurant, COLLECT(DISTINCT f.name) AS foods

取回餐馆和他们的食物。

相关内容

  • 没有找到相关文章

最新更新