Porjaz
Porjaz

Reputation: 791

Neo4j return subgraph based on certain id

My database stores family relationships.

What I am trying to do is to return the whole family tree of a person with a certain ID.

I have tried the following query:

MATCH (p:Person {id:'1887'})-[r*1..3]-(k:Person)
RETURN distinct(p), r, k

but I didn't get the result that I wanted.

To make it more clear, I would like to get all the relationships that a person with specified ID has, along with the relationships of all the other nodes that he is connected to.

To explain further, here is an example:

Boris is a parent of Mike and Anna. Apart of seeing Boris' relationship to them, I also want to see Mike and Anna's further relationships with their subgraphs.

Upvotes: 3

Views: 569

Answers (1)

Bruno Peres
Bruno Peres

Reputation: 16355

I believe you should try returning the complete path, like this:

MATCH path = (p:Person {id:'1887'})-[r*1..3]-(k:Person)
RETURN path

This query will store each the path between a person with id = 1887 and another person with depth 1 to 3 and return it. Or if you are interested only in the nodes you can extract it with the nodes() function:

MATCH path = (p:Person {id:'1887'})-[r*1..3]-(k:Person)
RETURN nodes(path)

Upvotes: 2

Related Questions