2013-02-15 57 views
1

你好,這裏是我當前的查詢,我想「再過濾器」:濾波過濾一個暗號查詢結果

START movie = node(*) 
MATCH user-[:LIKE]->category-[:SIMILAR*0..3]-()<-[:TAGGED]->movie 
WHERE user.name = "current_user" 

WITH DISTINCT movie, user, category 

RETURN user.name, category.name, ID(movie), movie.name 
ORDER BY movie.name; 

http://console.neo4j.org/r/u19iim

這是它看起來像當前查詢後:

+--------------+----------------+-----------+-------------------------+ 
| user.name | category.name | ID(movie) | movie.name    | 
+--------------+----------------+-----------+-------------------------+ 
| current_user | c    | 14  | movie_c_and_d_and_e  | 
| current_user | d    | 14  | movie_c_and_d_and_e  | 
| current_user | e    | 14  | movie_c_and_d_and_e  | 
| current_user | a    | 9   | movie_of_a_and_b_and_b1 | 
| current_user | b    | 9   | movie_of_a_and_b_and_b1 | 
| current_user | b    | 10  | movie_of_b2_first  | 
| current_user | b    | 11  | movie_of_b2_second  | 
| current_user | c    | 12  | movie_of_c    | 
| current_user | d    | 13  | movie_of_d_and_e  | 
| current_user | e    | 13  | movie_of_d_and_e  | 
+--------------+----------------+-----------+-------------------------+ 

我想GROUP BY COUNT(sugg) AS category_count提取此:

+--------------+----------------+-----------+-------------------------+ 
| user.name | category_count | ID(movie) | movie.name    | 
+--------------+----------------+-----------+-------------------------+ 
| current_user | 3    | 14  | movie_c_and_d_and_e  | 
| current_user | 2    | 9   | movie_of_a_and_b_and_b1 | 
| current_user | 2    | 13  | movie_of_d_and_e  | 
| current_user | 1    | 10  | movie_of_b2_first  | 
| current_user | 1    | 11  | movie_of_b2_second  | 
| current_user | 1    | 12  | movie_of_c    | 
+--------------+----------------+-----------+-------------------------+ 

我該如何做到這一點?

類似的問題: - how to have two aggregation in cypher query in neo4j?

更新
這裏的工作結果(有示範:http://tinyurl.com/cywlycc):

START movie = node(*) 
MATCH user-[:LIKE]->category-[:SIMILAR*0..3]-()<-[:TAGGED]->movie 
WHERE user.name = "current_user" 
WITH DISTINCT movie, category WITH COUNT(movie) AS category_count, movie, collect(category.name) as categorized 
RETURN category_count, ID(movie), movie.name, categorized 
ORDER BY category_count DESC; 

回答

2
START movie = node(*) 
MATCH user-[:LIKE]->category-[:SIMILAR*0..3]-()<-[:TAGGED]->movie 
WHERE user.name = "current_user" 
WITH DISTINCT movie, user, category 
RETURN user.name, count(category.name) as category_count, ID(movie), movie.name 
ORDER BY category_count desc, movie.name asc 

http://console.neo4j.org/r/69rfkn

+0

您的解決方案作品,並回答我的問題,我的查詢有點恐怖我發現可以放置如下結果的WITH語句:START movie = node(*)MATCH user - [:LIKE] - > category - [:SIMILAR * 0..3] - ()<-[:TAGGED]->電影WHERE user.name =「current_user」WITH DISTINCT電影,類別WITH COUNT(電影)AS category_count,movie,collect(category.name)作爲分類RETURN category_count,ID(電影),movie.name,分類 ORDER按category_count DESC;所以我可以有效地'重新篩選'我的結果。謝謝! http://tinyurl.com/cywlycc – 2013-02-15 23:49:51

+1

是的,這就是爲什麼,你也可以應用順序和限制每個與聚合,然後過濾聚合後。也許把更新後的查詢放在你的問題中,以便更好地格式化和可讀。 – 2013-02-20 07:35:16