5

我有一个时间线类型查询,用于检索帖子和“喜欢”帖子的用户。

START me=node:node_auto_index(UserIdentifier='USER0')
MATCH me-[rels:FOLLOWS*0..1]-myfriend
WITH myfriend
MATCH myfriend-[:POSTED*]-statusupdates<-[r?:LIKE]-likers
WHERE myfriend <> statusupdates
RETURN distinct statusupdates, FILTER (x in collect(distinct likers) : x <> null), myfriend
ORDER BY statusupdates.PostTime DESC
LIMIT 25; 

我将检索到的帖子数量限制为 25 个。我还想限制喜欢帖子的用户数量。有没有办法在查询中使用多个限制子句?理想情况下,我想做以下事情:

START me=node:node_auto_index(UserIdentifier='USER0')
MATCH me-[rels:FOLLOWS*0..1]-myfriend
WITH myfriendMATCH myfriend-[:POSTED*]-statusupdates<-[r?:LIKE]-likers
WHERE myfriend <> statusupdates
RETURN distinct statusupdates, LIMIT FILTER (x in collect(distinct likers) : x <> null) 6, myfriend
ORDER BY statusupdates.PostTime DESC
LIMIT 25; 

或者:

START me=node:node_auto_index(UserIdentifier='USER0')
MATCH me-[rels:FOLLOWS*0..1]-myfriend
WITH myfriendMATCH myfriend-[:POSTED*]-statusupdates<-[r?:LIKE]-likers
WHERE myfriend <> statusupdates
RETURN distinct statusupdates, FILTER (x in collect(distinct likers) : x <> null), myfriend
LIMIT likers 6
ORDER BY statusupdates.PostTime DESC
LIMIT 25; 

这会将每个帖子的返回点赞数限制为 6。我怎样才能实现这一点?

4

2 回答 2

9

限制喜欢者的问题在于它位于查询的另一端,因此无法优化它。你基本上必须做两场比赛。此外,WITH 后的 LIMIT 仅在 1.9.M01 中可用

所以,我认为这种做你想要的:

START me=node:node_auto_index(UserIdentifier='USER0')
MATCH me-[rels:FOLLOWS*0..1]-myfriend-[:POSTED*]-statusupdates<-[r?:LIKE]-likers
WITH distinct likers
// you can also order by something here, if you want.
LIMIT 6
START me=node:node_auto_index(UserIdentifier='USER0')
// at this point, likers is already bound, so it's limited to the 6
MATCH me-[rels:FOLLOWS*0..1]-myfriend-[:POSTED*]-statusupdates<-[r?:LIKE]-likers
RETURN distinct statusupdates, likers, myfriend
ORDER BY statusupdates.postTime
LIMIT 25;

未经测试的代码。希望它有效——下次在控制台中为我们构建一个示例,以便我们可以玩耍。:)

于 2012-11-16T00:12:10.017 回答
8

在 Neo4j 2.0 中,您可以使用集合切片。即你可以做类似的查询

MATCH (n)-[r*0..1]-(x) RETURN n, LABELS(n), COLLECT([x,id(x),LABELS(x),r])[0..10] LIMIT 5

上面的示例将返回集合中最多 5 个 n 节点,每个节点包含 0 到 10 个相关节点。

于 2014-07-26T05:57:01.917 回答