gpt4 book ai didi

Neo4j : How to combine multiple cyphers into one?

转载 作者:行者123 更新时间:2023-12-02 21:11:50 26 4
gpt4 key购买 nike

我有一个带有通常社交图的 Neo4j 数据库。我正在尝试创建一个事件源(用户 X 关注了您,用户 Y 喜欢了您的消息等)

UserMessage节点。

以及关注喜欢提及关系,每个关系都有一个created_on属性。

(实际上我还想要更多关系,但为了简洁起见,我将其保留为 3 个)

我正在尝试返回事件事件列表,按日期分页和排序。每个事件都会有发生的日期、与之相关的用户以及消息(如果适用)。

我对每种事件类型/关系都适用这种方法,但我正在努力将它们全部组合成一个密码/响应。

Cyphers 分别是...

喜欢

MATCH (me:User {username:"matt"})-[:Owner]->(msg:Message)<-[likes:Likes]-(user:User) RETURN user, msg, likes.created_on ORDER BY likes.created_on LIMIT 25

提及

MATCH (me:User {username:"matt"})<-[mentions:Mentions]-(msg:Message)-[:Owner]-(user:User) RETURN user, msg, mentions.created_on ORDER BY mentions.created_on LIMIT 25

已关注

MATCH (me:User {username:"Matt"})<-[follows:Follows]-(user:User) RETURN user, follows.created_on ORDER BY follows.created_on LIMIT 25

我最初将它们全部与可选组合在一起,但这每行返回多个结果/关系类型。因此,一个 Activity 可以同时具有评论提及。这会扰乱限制请求,因为我们有 2 个事件占用一个结果。

然后我尝试了 WHEREOR

 MATCH (me:User {username:"Matt"})-[r]-(m)--(u)
WHERE me-[:Owner]->(m:Message)<-[r:Likes]-(u:User)
OR me<-[r:Message]-(m:Message)-[:Owner]-(u:User)
OR me<-[r:Follows]-(u:User)
RETURN m, u, r.created_on
ORDER BY r.created_on
LIMIT 25

但是我现在迷路了!这似乎根本没有返回任何结果

关于如何将上述 Cyphers 合并到一个请求中,有什么建议/指示吗?

最佳答案

尝试这样的事情

MATCH (me:User {username:"matt"})
OPTIONAL MATCH (me)-[:Owner]->(msg:Message)<-[likes:Likes]-(user:User)
WHERE likes.created_on > x-days-ago
WITH me, msg, user, likes.created_on as created_on
ORDER BY created_on LIMIT 25
WITH me, collect(distinct {user:user, created_on:created_on}) as activities
OPTIONAL MATCH (me)<-[mentions:Mentions]-(msg:Message)-[:Owner]->(user:User)
WITH me, msg, user, mentions.created_on as created_on, activities
ORDER BY created_on LIMIT 25
WITH me, activities + collect(distinct {user:user, created_on:created_on}) as activities
OPTIONAL MATCH (me)<-[follows:Follows]-(user:User)
WITH me, msg, user, follows.created_on as created_on, activities
ORDER BY created_on LIMIT 25
UNWIND (activities + collect(distinct {user:user, created_on:created_on})) as activity
RETURN activity
ORDER BY activity.created_on LIMIT 25

关于Neo4j : How to combine multiple cyphers into one?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33035382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com