如何限制密码中的整体联合

问题描述

我有这些节点:

  1. user{user_id}: users
  2. thread{thread_id,post_date} : posts
  3. tag_id{tag_id}: the tag of the post

还有这些关系:

  1. (user) - [: FOLLOWED] -> (tag) // 用户跟随标签
  2. (thread) - [: BELONG_TO] -> (tag) // 帖子属于标签
  3. (user) - [: READ{read_date}] -> (thread) // 用户阅读帖子
  4. (user) - [: BEING_REPLIED{post_date}] -> (thread) // 另一个用户对他/她在帖子中的评论给予了回复
  5. (user) - [: BEING_MENTIONED{post_date}] -> (thread) // 用户在帖子中被另一个用户评论提及

我想获得该用户回复或被其他用户提及的 10 个帖子,然后对于属于该用户关注的标签但该用户尚未阅读以显示在每个用户的提要中的帖子,我在查询但不能限制为总数,结果形式限制为最后一个联合 我写的密码如下:

MATCH (u:User {user_id:3})-[rp:BEING_REPLIED]->(th:Thread)<-[r:READ]-(u:User {user_id:3})
WHERE rp.post_date> r.read_date
return u.user_id as user_id,th.thread_id as thread_id,duration.inDays(datetime(),datetime(rp.post_date)).days*10 + 1000000 AS point
UNION ALL

MATCH (u:User {user_id:3})-[m:BEING_MENTIONED]->(th:Thread)<-[r:READ]-(u:User {user_id:3})
WHERE m.post_date> r.read_date
return u.user_id as user_id,datetime(m.post_date)).days*10 + 1000000 AS point
UNION ALL

MATCH (u:User {user_id:3})-[m:BEING_MENTIONED]->(th:Thread)
WHERE NOT EXISTS ((u)-[:READ]->(th))
return u.user_id as user_id,datetime(m.post_date)).days*10 + 1000000 AS point

MATCH (u:User)-[:FOLLOWED]->(t:Tag)<-[:BELONG_TO]->(th)
WHERE u.user_id = 3 AND NOT EXISTS((u)-[]->(th))
WITH u.user_id AS user_id,th.thread_id AS thread_id,(0.5*th.like_count + 0.3*th.comment_count + 0.005*th.view_count
+ duration.inDays(datetime(),datetime(th.published_date)).days*100) AS point
ORDER BY point desc
RETURN disTINCT user_id,thread_id,point
UNION
MATCH (u:User)-[:FOLLOWED]->(t:Tag)<-[:BELONG_TO]->(th)
WHERE u.user_id = 3 AND NOT EXISTS((u)-[]->(th))
AND NOT th.rating_total  IS NULL
WITH u.user_id AS user_id,(duration.inDays(datetime(),datetime(th.published_date)).days*150 + 30*th.rating_total) AS point
ORDER BY point desc,th.published_date desc
RETURN disTINCT user_id,point
LIMIT 10

如何整体设置此查询限制?

感谢您的帮助!

解决方法

你需要子查询,你应该使用 Neo4j 4.0.x 或更高版本,这允许你执行 post-UNION processing

在子查询中使用 UNION ALL,在它之外使用 LIMIT 10,应该能让你得到你想要的。