Cypher Neo4j ORDER BY DESC查询

时间:2014-10-13 14:03:38

标签: neo4j cypher

我想按降序排列COUNT(Movie.title)。 但它给出了一个错误。 这是查询。

MATCH (Movie {genre:"Action"})<-[:ACTS_IN]-(Person)
                 "RETURN Person.name, Movie.genre,  COUNT(Movie.title)"
                 "ORDER BY COUNT(Movie.title) DESC"
                 "LIMIT 100";

谢谢!

2 个答案:

答案 0 :(得分:9)

您可以使用此查询:

MATCH (movie:Movie {genre:"Action"})<-[:ACTS_IN]-(person:Person)
RETURN person.name, movie.genre,  COUNT(distinct movie.title) AS cnt
ORDER BY cnt DESC
LIMIT 100

答案 1 :(得分:1)

返回错误是因为您无法立即在Cypher中按聚合顺序。要按任何汇总排序,您需要使用WITH运算符。

所以你的查询应该是(假设你想列出每个类型的每个演员的标题):

MATCH (Movie {genre:"Action"})<-[:ACTS_IN]-(Person)
RETURN Person.name, Movie.genre,  COUNT(Movie.title)
WITH Person.name AS name, Movie.genre AS genre, COLLECT(Movie.title) AS titles
RETURN name, genre, titles
ORDER BY LENGTH(titles) DESC
LIMIT 100

限制100现在已经改变了它的行为,所以你可能想把它移到查询中:

MATCH (Movie {genre:"Action"})<-[:ACTS_IN]-(Person)
RETURN Person.name, Movie.genre,  COUNT(Movie.title)
WITH Person, Movie
LIMIT 100
WITH Person.name AS name, Movie.genre AS genre, COLLECT(Movie.title) AS titles
RETURN name, genre, titles
ORDER BY LENGTH(titles) DESC

除此之外:为了让你的查询表现良好,你应该在Movie.genre属性上有一个索引,你应该为Movie和Person引入标签。