Cypher: Travel in several crossed chained list

两盒软妹~` 提交于 2019-12-11 14:14:31

问题


I have a graph with users. In this graph their are also activities (nodes). Each user have a chained list of its activities :

(user)-[:NEXT_ACTIVITY*]->(activities)

But there is one tricky part : an activity can be owned by 2 users. So I set a property "idUser" in my NEXT_ACTIVITY relation, with the ID of the user. This is how I can find all activities for 1 user.

START user=node:users('name:JohnDoe')
MATCH (user)-[rel:NEXT_ACTIVITY*]->(activities)
WHERE ALL (r in rel WHERE r.idUser = id(user))

My problem is that I don't know how to get activities for several users with this system. How can I travel chained list of several users, and being sure that all NEXT_ACTIVITY the graph travel belongs to these users ?

The use case : I want to get activities of a user and its friends.

Regards, Raphaël

Ps: the reason why an activity can be owned by 2 users is that if A publish on B's wall, it's an activity for A and B.


回答1:


You can first get all friends of the user(say name='u1'), that includes the user as well since the length of the relationship is 0..1, and then for each of them, get all activities.

Match u:User-[:FRIEND*0..1]->friend:User
Where u.name = 'u1'
With friend 
Match friend-[rel:NEXT_ACTIVITY*]->activity
Where all(r in rel where r.idUser = id(friend))
Return friend.name, collect(activity.name)

if you just need all activities regardless of users, then "Return distinct activity.name"



来源:https://stackoverflow.com/questions/19032147/cypher-travel-in-several-crossed-chained-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!