我正试图在各种关系中创建活动源。目前唯一的方法是使用UNION
运行三个语句。然而,你不能在所有联合的结果上LIMIT
和ORDER
- 目前看来这是Neo的一个限制。
有人知道我如何重写下面的内容,以便我可以对整个结果进行排序和限制吗?
match (me:User {username:'bob'})-[:Owner]->(m:Message)<-[r:Likes|:Reply|:Share]-(u:User)
return m as message, u as user, lower(type(r)) as activity, r.created_on as date
order by date skip 0 limit 25
union match (me:User {username:'bob'})<-[r:Mentions]-(m:Message)-[:Owner]-(u:User)
return m as message, u as user, lower(type(r)) as activity, r.created_on as date
order by date skip 0 limit 25
union match (me:User {username:'bob'})<-[r:Follows]-(u:User)
return NULL as message, u as user, lower(type(r)) as activity, r.created_on as date
order by date skip 0 limit 25
我找到了以下代码,它返回一个包含所需数据嵌套属性的单列,但我无法想出如何对最终集合进行排序.....
match (me:User {username:'bob'})-[:Owner]->(m:Message)<-[r:Likes|:Reply|:Share]-(u:User)
with collect({activity:lower(type(r)), user:u, message:m, date:r.created_on}) as a1
optional match (me:User {username:'bob'})<-[r:Mentions]-(m:Message)-[:Owner]-(u:User)
with collect({activity:lower(type(r)), user:u, message:m, date:r.created_on }) as a2, a1
optional match (me:User {username:'bob'})<-[r:Follows]-(u:User)
with collect({activity:lower(type(r)), user:u, date:r.created_on }) as a3, a2, a1
with a3 + a2 +a1 as all
unwind all as activity
return activity
skip 0 limit 25
任何帮助都非常感激!
更新
现在我有了这个……
MATCH (me:User { username:'bob' })--(u:User)
OPTIONAL MATCH (me)-[:Owner]->(m:Message)<-[r:Likes|:Reply|:Share]-(u)
WITH me, collect({ type:lower(type(r)), user:u, message:m, date:r.created_on }) AS a1
OPTIONAL MATCH (me)<-[r:Mentions]-(m:Message)<-[:Owner]-(u)
WITH me, collect({ type:lower(type(r)), user:u, message:m, date:r.created_on }) AS a2, a1
OPTIONAL MATCH (me)<-[r:Follows]-(u)
WITH collect({ type:lower(type(r)), user:u, date:r.created_on })+ a2 + a1 AS all
UNWIND all AS activity
WITH activity
WHERE activity.type is not null
RETURN activity
ORDER BY activity.date
LIMIT 25;
你能看到这个代码有任何性能问题吗?
我在开头匹配了me--user,以确保只查找我与之存在某种关系的用户。然后在结尾过滤掉可选匹配中的NULL匹配。因为我手动收集了一个文字对象,如果没有匹配到任何内容,它会得到一个具有NULL条目的对象,所以我最后将其移除....
所有这些都是因为不能对POST UNION进行过滤!
WITH
之间有三个不同的OPTIONAL MATCH
子句,所以我认为在这种情况下它将会扫描三次User
标签。 - Brian Underwoodme
和user
之间的任何关系,然后再选择性地匹配Likes
、Reply
和Share
关系,因为可能没有喜欢、回复或分享链接...MATCH (me:User { username:'bob' })--(u:User) OPTIONAL MATCH (me:User { username:'bob' })-[:Owner]->(m:Message)<-[r:Likes|:Reply|:Share]-(u)
- Matt BrysonMATCH
子句来定义一组可供选择的备选路径。你可以使用WHERE (path) OR (path) OR (path)
,但我认为这可能会有相同的性能问题,并且你仍然需要在OPTIONAL MATCH
中重复路径以定义变量,以便返回。 - Brian UnderwoodMATCH (me:User { username:'bob' }), (user:User)
,我会得到与me
没有关系的结果。那么如何使用可选路径呢?所以我想匹配我和一个用户,但只有他们通过示例中的一个或多个路径相关联时才匹配。 - Matt Bryson