如何解决Cypher查询中的UNWIND语句
我有这些节点:
-
user{user_id}: users
-
thread{thread_id,post_date} : posts
-
tag_id{tag_id}: the tag of the post
这些关系:
-
(user) - [: FOLLOWED] -> (tag)
//用户关注标签 -
(thread) - [: BELONG_TO] -> (tag)
//帖子属于标签 -
(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
但是它不起作用。帮帮我
解决方法
-
如果您不打算写入数据库,请
- 不要使用MERGE。 (而且,如果您确实要写入数据库,则必须 学习如何正确使用
MERGE
以避免不必要的结果。) - 请勿使用aggregating functions(例如
COUNT
),除非您已阅读文档并了解如何使用它们。特别是,您必须了解什么是“分组键”以及它们如何影响聚合函数的行为。 - 您应该学习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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。