繁体   English   中英

Neo4j管道数据

[英]Neo4j pipe data

嗨,我在neo4j上,遇到了一些麻烦,我有一个查询要返回一个具有最高百分比的a节点(烹饪),如下所示

// 1. Find the most_popular_cuisine
MATCH (n:restaurants)
WITH COUNT(n.cuisine) as total
MATCH (r:restaurants)    
RETURN r.cuisine , 100 * count(*)/total as percentage
order by percentage desc
limit 1

我正在尝试通过获得最佳结果并与之匹配以进一步获得具有该属性的节点来进一步扩展此范围

WITH COUNT(n.cuisine) as total
MATCH (r:restaurants)    
WITH r.cuisine as cuisine , count(*) as cnt
MATCH (t:restaurants)
WHERE t.cuisine = cuisine AND count(*) = MAX(cnt)
RETURN t

我认为您最好对模型进行一些重构,例如:Cuisine是标签,并且每种菜式都有其自己的节点。

(:Restaurant)-[:OFFERS]->(:Cuisine)

要么

(:Restaurant)-[:SPECIALIZES_IN]->(:Cuisine)

然后您的查询可以像这样

MATCH (cuisine:Cuisine)
RETURN cuisine, size((cuisine)<-[:OFFERS]-()) AS number_of_restaurants
ORDER BY number_of_restaurants DESC  

我无法在WITH而不是RETURN语句WITH r.cuisine as cuisine , count(*) as cntWITH ,因此我不得不求助于稍微冗长的方法。

可能会有一种更优化的方法来执行此操作,但这也可行,

// Get all unique cuisines in a list
MATCH (n:Restaurants)  
WITH COUNT(n.Cuisine) as total, COLLECT(DISTINCT(n.Cuisine)) as cuisineList

// Go through each cuisine and find the number of restaurants associated with each
UNWIND cuisineList as c
MATCH (r:Restaurants{Cuisine:c})
WITH total, r.Cuisine as c, count(r) as cnt
ORDER BY cnt DESC
WITH COLLECT({Cuisine: c, Count:cnt}) as list

// For the most popular cuisine, find all the restaurants offering it
MATCH (t:Restaurants{Cuisine:list[0].Cuisine}) 
RETURN t

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM