我在neo4j中创建了以下图表。这里user1与user2和user3交换了消息
(user1)-[:EMAIL_SENT]->(Email)-[:EMAIL_TO]->(user2)
(user1)<-[:EMAIL_TO]-(Email)<-[:EMAIL_SENT]-(user2)
(user1)-[:EMAIL_SENT]->(Email)-[:EMAIL_TO]->(user3)
(user1)<-[:REPLY_TO]-(Email)<-[:REPLY_SENT]-(user3)
(user1)-[:REPLY_SENT]->(Email)-[:REPLY_TO]->(user3)
我想检索user1的facebook样式结果,即显示每个参与者的最新消息(发送或接收)。下面的查询显示了user1和哪个参与者发送和接收的所有消息,但我想汇总每个参与者的结果。
MATCH (U:User {username:'user1'})
-[L:EMAIL_SENT|EMAIL_TO]-(E:email)-[R:EMAIL_SENT|EMAIL_TO]-
(P:User)
WHERE type(L)<>type(R)
RETURN E.text as text,
E.subject as subject,
id(E) as message_id,
U.username as user,
P.username as participator,
(CASE type(L) WHEN 'EMAIL_SENT' THEN 'out' ELSE 'in' END) as direction
如果我尝试这样的话
MATCH (U:User {username:'user1'})
-[L:EMAIL_SENT|EMAIL_TO]-(E:email)-[R:EMAIL_SENT|EMAIL_TO]-
(P:User)
WHERE type(L)<>type(R)
RETURN E.text as text,
E.subject as subject,
id(E) as message_id,
U.username as user,
P.username as participator,
(CASE type(L) WHEN 'EMAIL_SENT' THEN 'out' ELSE 'in' END) as direction ORDER BY E.timestamp DESC,collect (E.text)
我收到错误 - “如果在前面的RETURN中没有聚合表达式(第5行,第1列(偏移量:187))”RETURN E.text as text,“
,则无法在ORDER BY中使用聚合此外,我不确定如何在使用“收集”检索所有发送给特定参与者的电子邮件之前按时间戳排序参与者对所有电子邮件进行分组
答案 0 :(得分:2)
1)订购时,您不能将收集作为您需要安排的财产。
2)尝试这样的事情:
// All interaction between the user `user1` and his partner
MATCH (U:User {username:'user1'})
-[L:EMAIL_SENT|EMAIL_TO|REPLY_TO|REPLY_SENT]-
(E:Email)
-[R:EMAIL_SENT|EMAIL_TO|REPLY_TO|REPLY_SENT]-
(P:User)
WHERE type(L)<>type(R)
// Sorted by time and get the type of interaction (the direction of)
WITH U, P, E, type(L) as D ORDER BY E.timestamp DESC
// Get collect of interactions (email and direction) by partner
WITH U, P, head( collect( {email: E, direction: D} ) ) as lastInteraction
// Return last interaction between user and his partner
RETURN U as User,
P as Partner,
lastInteraction['email']['subject'] as subject,
lastInteraction['email']['text'] as text,
lastInteraction['direction'] as direction
ORDER BY lastInteraction['email']['timestamp'] DESC
答案 1 :(得分:1)
这对你有用吗?
MATCH (U:User {username:'user1'})-[L:EMAIL_SENT|EMAIL_TO]-(E:email)--(P:User)
WITH U, L, E, P
ORDER BY E.timestamp DESC
RETURN E.text as text,
E.subject as subject,
id(E) as message_id,
U.username as user,
P.username as participator,
(CASE type(L) WHEN 'EMAIL_SENT' THEN 'out' ELSE 'in' END) as direction;
它通过在 RETURN
子句之前降序时间戳来对电子邮件进行排序。它还简化了原始的MATCH/WHERE
条款。