Thomas
Thomas

Reputation: 235

Find persons with connection to other person

I'm playing with Neo4J, and trying to find all persons who has a connection with a person.

What I want is to have a (p:Person) and the look at all relations, and follow them, until I find an other (p2:Person), and the return all (p2). There can be diffrent rutes from (p) to (p2). A couple of exampels would be:

(p:Person)-->(:Tweet)-[:mentions]->(p2:Person)
(p:Person)-->(:Tweet)-[:in_reply_to]->(:Tweet)-[:mentions](p2:Person)
(p:Person)-[:follows]->(p2:Person)

And a lot of other cases. More general, the point is, I want to follow a relation from (p:Person) until it finds a (p2:Person) or the node has no more outgoung, unused relations

Upvotes: 2

Views: 85

Answers (1)

Brian Underwood
Brian Underwood

Reputation: 10856

You could try this

(p:Person)-[:mentions|in_reply_to|mentions*1..5]->(p2:Person)

That would match all paths of length 1 to 5 matching those relationship types. You can also take out the directionality if you don't care about that. You'll probably want to assign it to a path variable and then return that, like this:

MATCH path=(p:Person)-[:mentions|in_reply_to|mentions*1..5]->(p2:Person)
RETURN path

If you want to find the shortest path between two people:

MATCH path=shortestPath((p:Person)-[:mentions|in_reply_to|mentions*1..5]->(p2:Person))
RETURN path

Upvotes: 2

Related Questions