简体   繁体   English

Neo4j Cypher:使用LIMIT和COLLECT(或在同一查询中使用LIMIT两次)

[英]Neo4j Cypher: Using LIMIT and COLLECT (or using LIMIT twice in the same query)

I have a timeline type query that retrieves posts and athe users who have 'liked' a post. 我有一个时间轴类型查询,用于检索帖子以及“喜欢”帖子的用户。

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; 

I limit the number of posts that I retrieve to 25. I would also like to limit the number of users who have liked a post. 我将检索到的帖子数量限制为25.我还想限制喜欢帖子的用户数量。 Is there a way to use multiple limit clauses in a query? 有没有办法在查询中使用多个限制子句? Ideally I would like to do something like the following: 理想情况下,我想做类似以下的事情:

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; 

Or: 要么:

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; 

Which would limit the number of returned likers for each post to 6. How can I achieve this? 这会将每个帖子的返回者的数量限制为6.如何实现这一目标?

The trouble with limiting likers is that it's on the other end of the query, so there's no way to optimize that. 限制类似的问题在于它位于查询的另一端,因此无法对其进行优化。 You basically have to do two matches. 你基本上要做两场比赛。 Also, the LIMIT after the WITH is only available in 1.9.M01 此外,WITH之后的LIMIT仅在1.9.M01中可用

So, I think this sort of does what you want: 所以,我认为这样做符合你的要求:

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;

Untested code. 未经测试的代码。 Hope it works--next time build us a sample in console so we can play around. 希望它有效 - 下次在控制台中为我们建立样本,以便我们可以玩。 :) :)

In Neo4j 2.0 you can use collection slices. 在Neo4j 2.0中,您可以使用集合切片。 Ie you can do queries similar to 即你可以做类似的查询

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

The example above would return up to 5 n-nodes with 0 to 10 related nodes each in the collection. 上面的示例将返回最多5个n节点,每个节点在集合中各有0到10个相关节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM