Cypher /检索每个主题的最新通知

时间:2014-12-28 23:30:36

标签: neo4j cypher graph-databases

让我们假设这个模型:

(notification)-[:CONCERNS]->(user)

为了简单起见,让我们想象每个notification与特定主题相关的聊天条目相关。
因此,当用户进入Football主题并输入:"嗨大家好!"时,最终创建的notification包含这些属性(包括其他属性):

  • topidId(String)
  • occurredOn(时间戳(长))
  • body(字符串 - 聊天消息的摘录)

请注意,notificationtopic之间没有直接关系 事实上,在未来,我不想用用户的通知机制污染Neo4j。
Redis或RDBMS可以更好地处理它。

基本上,如果我运行此查询:

MATCH (user:User {_id: "30c62b1f-4556-4966-b60c-3547d4c8d522"})
WITH user 
MATCH user<-[:CONCERNS]-(n:Notification)
RETURN n.topicId AS TopicId, n.body as Body
ORDER BY n.occurredOn DESC

它可以返回:

TopicID     Body
123         Fine and you !  // I want to retain only this for Football (123 being Football)
123         Hey! How are you?   
456         Yes, I'm here !  // I want to retain only this for tennis (456 being Tennis)
456         Are you here?

如何更改查询以获取此结果,作为每个主题的最新消息。

  123     Fine and you !
  456     Yes, I'm here !

1 个答案:

答案 0 :(得分:0)

这个怎么样?

MATCH (user:User {_id: "30c62b1f-4556-4966-b60c-3547d4c8d522"})
MATCH user<-[:CONCERNS]-(n:Notification)
WITH user, collect(n) AS notifications
ORDER BY n.occurredOn DESC
WITH user, notifications[0] AS notification
RETURN notification.topicId AS TopicId, notification.body as Body
ORDER BY notification.occurredOn DESC