Cypher查询DISTINCT

时间:2013-06-14 23:30:03

标签: neo4j aggregate cypher

我有简单的用例但仍无法找到解决方案....
我有问题节点,每个问题都有类别,每个类别都有很多问题。 我想做以下事情:

检索5个问题,其中每个问题来自不同类别。

尝试以下但是它不是我正在寻找的因为我仍然从同一类别中得到问题。

 START question=node:__types__(className = "com.socialist.server.graph.entities.Question")
 RETURN distinct(question.category), question
 LIMIT 5

该用例的正确查询是什么? 你的答案很受欢迎。

1 个答案:

答案 0 :(得分:1)

这样的事情可能有用,但是如果每个类别都有很多,那么效率就不会很高:

START question=node:__types__(className = "com.socialist.server.graph.entities.Question")
RETURN question.category, head(collect(question))
LIMIT 5

此外,很快(希望通过2.0发布)将有一个很好的方法从集合中获取随机项目,如下所示:

START question=node:__types__(className = "com.socialist.server.graph.entities.Question")
WITH question.category as category, collect(question) as questions
RETURN category, questions[rand() * length(questions)]
LIMIT 5