【问题标题】:how to use ORDER BY and aggregating results together in cypher query如何在密码查询中使用 ORDER BY 和聚合结果
【发布时间】:2016-05-22 21:44:48
【问题描述】:

我在 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)

我想检索用户 1 的 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))中没有聚合表达式,则不能在 ORDER BY 中使用聚合,“RETURN E.text as text”,

另外,我不知道如何在使用收集检索所有电子邮件给特定参与者之前按时间戳排序后按参与者对所有电子邮件进行分组

【问题讨论】:

    标签: neo4j


    【解决方案1】:

    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
    

    【讨论】:

    • 不能返回文本(email.text)等邮件的属性吗?像 U.username 作为用户
    • 现在显示方向为空。在上一个查询中,它显示方向为 REPLY_TO 或 REPLY_SENT 或 EMAIL_SENT 或 EMAIL_TO
    • 嘿@stdob--你能给我发一封电子邮件给我neo4j.com的michael吗?
    • @MichaelHunger 是的,我做到了。
    【解决方案2】:

    这对你有用吗?

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

    【讨论】:

      猜你喜欢
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多