简体   繁体   English

Neo4j管道数据

[英]Neo4j pipe data

Hi there I am on neo4j and I am having some trouble I have one query where I want to return a the a node (cuisine) with the highest percentage like so 嗨,我在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

I am trying to extend this even further by getting the top result and matching to that to get nodes with just that property like so 我正在尝试通过获得最佳结果并与之匹配以进一步获得具有该属性的节点来进一步扩展此范围

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

I think you might be better off refactoring your model a little bit such that a :Cuisine is a label and each cuisine has its own node. 我认为您最好对模型进行一些重构,例如:Cuisine是标签,并且每种菜式都有其自己的节点。

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

or 要么

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

Then your query can look like this 然后您的查询可以像这样

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

I wasn't able to use WITH r.cuisine as cuisine , count(*) as cnt in a WITH rather than a RETURN statement, so I had to resort to a slightly more long-winded approach. 我无法在WITH而不是RETURN语句WITH r.cuisine as cuisine , count(*) as cntWITH ,因此我不得不求助于稍微冗长的方法。

There might be a more optimized way to do this, but this works too, 可能会有一种更优化的方法来执行此操作,但这也可行,

// 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