neo4j how to work with two match

我是研究僧i 提交于 2020-01-04 09:05:24

问题


I have two queries.

First query is

match (user)-[r:CreatesChat]-(chatitems)

Second query is

match (chatitems)-[r:PartOf]-(teamschat)-[s:OwnedBy]-()

I want to return the first 3 users from the first query

And to return the first 3 teams from the second query

The goal is to check if users from first query have the teams of second query

My neo4j query is

match (user)-[r:CreatesChat]-(chatitems)
with user.id as uid,chatitems.id as chatid
order by uid desc
with collect([uid])[..3] as users,collect([chatid])[..3] as chats
UNWIND users AS idusers
match (chatitems)-[r:PartOf]-(teamschat)-[s:OwnedBy]-()
return idusers

This query return

Returned 133239 rows in 1360 ms, displaying first 1000 rows

But I execute the query

match (user)-[r:CreatesChat]-(chatitems)
with user.id as uid,chatitems.id as chatid
order by uid desc
with collect([uid])[..3] as users,collect([chatid])[..3] as chats
UNWIND users AS idusers
return idusers

idusers returned are right

Returned 3 rows in 539 ms.

How can I relate these two queries ?


回答1:


I think you want to collect both the top 3 users and the top 3 teams and then unwind over each collection. Something like this:

MATCH (user:User)-[:CreatesChat]->(chatitems:Chat)
WITH user ORDER BY user.id DESC LIMIT 3
WITH collect(user) AS users
MATCH (chatitems:Item)-[:PartOf]->(teamsChat:Team)-[:OwnedBy]-()
WITH users, teamsChat ORDER BY teamsChat.id DESC LIMIT 3
WITH users, collect(teamsChat) AS teams
UNWIND users AS user
UNWIND teams AS team
MATCH p=(chatitems:Item)-[:PartOf]-(team)-[:OwndedBy]-(user)
RETURN p


来源:https://stackoverflow.com/questions/38294427/neo4j-how-to-work-with-two-match

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