Neo4j,如何 return 来自不同选项卡组的前 n 个?

Neo4j, How to return the top n from diffreent tag groups?

我有一个代表 "Posts" 的数据库,它包含创建日期、标签、标题。一些帖子是其他帖子的答案,然后一些帖子是问题,其他帖子是答案。这里的任务是在每个不同的标签组中找到回答时间最短的问题。我如何实现这一目标?

我已经可以找到所有已回答的问题,每个标签的问题创建时间与其第一个答案的创建时间之间的时间差,但我无法return每个标签的回答时间最短的问题(每个标签组中的前 1 名)。我只能return所有的东西。

谁能帮我解决这个问题?

这是我的查询:

WITH ['geospatial', 'economics', 'usa', 'demographics'] AS topiclist
UNWIND topiclist AS topics
Match (p1:Posts)
UNWIND p1.Tags AS tags
WITH p1,trim(tags) AS tag
Where tag = topics
Match (p1)-[:PARENT_OF]->(p2:Posts)
WITH p1, p2.CreationDate - p1.CreationDate AS time,tag,p2
ORDER BY tag,time
Return p1.Title ,time,tag

感谢 Michael Hunger 的回答,解决了我的问题!!如果你有类似的问题,请查看他的回答。

输出样本:

returned result, the 3 colums represent : questions, time difference, tags

示例数据:

All data will be used looks like this image, the post with a parentId is an answer, its parent is its belonged question

存储数据的时候如果做trim就好了

WITH ['geospatial', 'economics', 'usa', 'demographics'] AS topiclist
UNWIND topiclist AS topics

Match (p1:Posts) where single(tag in p1.tags where trim(tag) = topic)
Match (p1)-[:PARENT_OF]->(p2:Posts)
WITH p1, p2.CreationDate - p1.CreationDate AS time,tag,p2
ORDER BY tag,time
with tag, head(collect({post:p1,time:time}) as first
Return first.post.Title as title,first.time as time,tag