2016-05-22 46 views
1

我在neo4j中創建了以下圖形。這裏USER1已經與用戶2和用戶3交換的消息如何使用ORDER BY並在密碼查詢中彙總結果

(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))「返回E.text文本,」

而且,我不知道如何使用收集檢索所有郵件到特定的參與者

之前時間戳排序後組由參與者所有的電子郵件

回答

2

1)訂購時,您不能將collect作爲您需要安排的財產。

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 
+0

是不是可以返回電子郵件的屬性,如文本(email.text)?像U.username一樣用戶 – jas

+0

當然這是可能的。看一下更新。 –

+0

它現在顯示方向爲空。在之前的查詢中,它顯示的方向爲REPLY_TO或REPLY_SENT或EMAIL_SENT或EMAIL_TO – jas

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子句。