Cypher查询中的UNWIND语句

时间:2020-08-16 05:20:35

标签: neo4j cypher

我有这些节点:

  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] -> (thread) //用户阅读帖子

现在,我要查询用户未读的5个帖子之后的每个标签。我写的密码如下:

MATCH (u:User)-[:FOLLOWED]->(t:Tag)
WHERE u.id = 39792
WITH collect(t) as tLists
UNWIND tLists as t
MERGE (u:User)-[:FOLLOWED]->(t:Tag)<-[:BELONG_TO]-(th:Thread)<-[r:READ]-()
WHERE not (u)-[:READ]->(th)
RETURN u.id, th.id, t.id, duration.inDays(datetime(), datetime({ epochmillis: apoc.date.parse(th.post_date)})).days as time, count(r) as count_view
ORDER BY time DESC, count_view DESC
LIMIT 5

但是它不起作用。帮帮我

1 个答案:

答案 0 :(得分:0)

    如果您不打算写入数据库,请
  1. 不要使用MERGE。 (而且,如果您确实要写入数据库,则必须 学习如何正确使用MERGE以避免不必要的结果。)
  2. 请勿使用aggregating functions(例如COUNT),除非您已阅读文档并了解如何使用它们。特别是,您必须了解什么是“分组键”以及它们如何影响聚合函数的行为。
  3. 您应该学习degreeness checks如何有效地计算某些关系模式。

此查询可能对您有用:

MATCH (u:User)-[:FOLLOWED]->(t:Tag)<-[:BELONG_TO]->(th)
WHERE u.id = 39792 AND NOT EXISTS((u)-[:READ]->(th))
RETURN u.id, th.id, t.id,
  duration.inDays(datetime(), datetime({ epochmillis: apoc.date.parse(th.post_date)})).days AS days,
  SIZE((th)<-[:READ]-()) as count_view
ORDER BY days DESC, count_view DESC
LIMIT 5

[更新]

这是一种获取5个最新线程的方式,每个线程紧随其后的是用户尚未阅读的最多视图:

MATCH (u:User)-[:FOLLOWED]->(t:Tag)<-[:BELONG_TO]->(th)
WHERE u.id = 39792 AND NOT EXISTS((u)-[:READ]->(th))
WITH t.id AS tagId, th.id AS threadId
ORDER BY apoc.date.parse(th.post_date) DESC, SIZE((th)<-[:READ]-()) DESC
RETURN tagId, COLLECT(threadId)[..5] AS threads
相关问题