Cypher查询查找发送的所有电子邮件并在neo4j

时间:2016-05-15 23:26:53

标签: neo4j

我在neo4j中创建了以下图表。

(user1)-(:EMAIL_SENT)->(email)-[:EMAIL_TO)->(user2)
(user1)<-(:EMAIL_TO)-(email)<-[:EMAIL_SENT)-(user2)
(user1)-(:EMAIL_SENT)->(email)-[:EMAIL_TO)->(user3)

此处user1已发送两封电子邮件并收到一封电子邮件。我想构建一个查询来检索user1发送和接收的所有电子邮件,以及发送和接收来自user1的电子邮件的其他用户的信息

我有此查询,但它只显示收到的电子邮件。我希望它扩展到包括发送的电子邮件。不知道该怎么做

 OPTIONAL MATCH (you:User)<-[:EMAIL_TO]-(Email),
          (they:User)-[:EMAIL_SENT]->(Email)
 WHERE you.username = user1
 RETURN Email.text AS text, 
        Email.subject AS subject, 
        Email.id AS message_id, 
        they.username AS emailfrom, 
        you.username AS loggedinuser

1 个答案:

答案 0 :(得分:1)

作为一种选择 - 两种不同的方式。

1)使用union查询组合:

MATCH (U:User {username:'user1'})-[:EMAIL_SENT]->(E:email)-[:EMAIL_TO]->(R:User)
RETURN E.text as text, 
       E.subject as subject, 
       id(E) as message_id, 
       U.username as emailfrom, 
       R.username as emailto
UNION
MATCH (U:User {username:'user1'})<-[:EMAIL_TO]-(E:email)<-[:EMAIL_SENT]-(R:User)
RETURN E.text as text, 
       E.subject as subject, 
       id(E) as message_id, 
       R.username as emailfrom, 
       U.username as emailto

2)根据关系类型确定电子邮件的方向:

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