cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Limit match results per row in path

alete89
Node Link

Hello Community,
I'm using Neo4j 4.1 and neo4j-driver for nodeJS.

My data looks like this:

2X_6_66b21221dd79c1c1351f4d4fe1d59c2a6b6b81e7.png
where INVITED_BY has a property 'wid' that indicates to what workspace a user invited another.

I'm using a frontend library to show nodes, and I'm working with all Cypher queries that returns paths, like:

match path=(n:Workspace)-[]-()
  where n.wid = '${nodeId}'
  return path
  limit 50`

This time I need to show top 'inviters', and show people invited by them, but limited to a few nodes.
I found this very useful link:

But I'm unable to adapt it to my model AND STILL return a path, instead of just the nodes.
If I run this query on Neo4j browser, it looks great:

match (promoter:Member)<-[r:INVITED_BY]-(invited:Member)
match (invited)-[r]->(promoter)
return promoter, collect(invited)[..5]

but the actual data returned (under table or text tab, or even just running the query with NodeJS driver) doesn't have the relationships.

I tried this, but it after I collect the nodes, I have a list, and isn't assignable to a node. And I'm stuck

match (promoter:Member)<-[r:INVITED_BY]-(invited:Member)
match path=(invited)-[r]->(promoter)
with collect(invited)[..5] as invi
match path2=(invi)-[r]->(promoter)
return path2

I just need to limit the invited nodes but still return a path.
Any help will be appreciated.

1 ACCEPTED SOLUTION

WIth Neo4j 4.1.x you have some good options here, we can use subqueries to LIMIT the paths to return. Something like this:

MATCH (promoter:Member)
WHERE (promoter)<-[:INVITED_BY]-()
CALL {
  WITH promoter
  MATCH path = (promoter)<-[r:INVITED_BY]-(invited:Member)
  WITH path
  LIMIT 5
  RETURN collect(path) as invitePaths
}
RETURN promoter, invitePaths

You can play with the path elements or what to return from the subquery, the point is that with the subquery you can use LIMIT and it will apply only to the matches from each promoter rather than limiting all result rows.

View solution in original post

2 REPLIES 2

WIth Neo4j 4.1.x you have some good options here, we can use subqueries to LIMIT the paths to return. Something like this:

MATCH (promoter:Member)
WHERE (promoter)<-[:INVITED_BY]-()
CALL {
  WITH promoter
  MATCH path = (promoter)<-[r:INVITED_BY]-(invited:Member)
  WITH path
  LIMIT 5
  RETURN collect(path) as invitePaths
}
RETURN promoter, invitePaths

You can play with the path elements or what to return from the subquery, the point is that with the subquery you can use LIMIT and it will apply only to the matches from each promoter rather than limiting all result rows.

alete89
Node Link

That's absolutely right. Thank you @andrew.bowman !