2017-11-19 3 views
2

こんにちは、私はのNeo4jの午前と私はいくつかの問題を抱えています私は私がしようとしていますので、のNeo4jパイプデータ

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

のような最も高い割合でノード(料理)を返すようにしたい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 

答えて

0

私はあなたが: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 
+0

雅問題は、モデルがdoc_managerを使用して、Mongoのインスタンスである、あまりにも作品かもしれません。私は大学の割り当てとして私は –

0

のように見えることができます私はWITHはなくRETURN文でWITH r.cuisine as cuisine , count(*) as cntを使用することができませんでしたので、私はしなければなりませんでした少し長めのアプローチを採用しています。

あり、これを行うために、より最適化された方法であるが、これは

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