Reputation: 211
I'm currently a beginner with Neo4J.
My graph looks like this for the moment:
I want to get all the Users (red nodes) from a certain User with a certain distance. Let's say I want all the Users with a distance of 2 from "Antoine". Currently, I've got this request:
MATCH (u1:User {firstname: 'Antoine'})-[:PARTICIPATE]->(e1:Event)<-[:PARTICIPATE]-(u2:User)-[:PARTICIPATE]->(e2:Event)<-[:PARTICIPATE]-(u3:User)
RETURN u3
Is there a way to add some genericity to this request ? If I want all the Users with a greater distance the request would be dirty...
I think this is not the correct way to do this. Is there a better way to achieve my goal ?
Upvotes: 1
Views: 456
Reputation: 30397
You need to use variable-length pattern matching.
With your graph, you need to traverse 2 :PARTICIPATE relationships to get to another :User node, so we can express adjacent users (via a single connecting node) like:
MATCH (u1:User {firstname: 'Antoine'})-[:PARTICIPATE*2]-(u2:User)
RETURN u2
And to get users at one more level distant like:
MATCH (u1:User {firstname: 'Antoine'})-[:PARTICIPATE*4]-(u3:User)
RETURN u3
We can get the set of both by using a range on the variable-length pattern, between 2 and 4 hops inclusive:
MATCH (u1:User {firstname: 'Antoine'})-[:PARTICIPATE*2..4]-(user:User)
RETURN user
Upvotes: 2