Pranav
Pranav

Reputation: 447

How can I get the relationship between start node and end node?

I'm trying to find family relations.

For example I want to know what's the relation between me and my father's wife? It should return mother.

What's the relation between me and my mother's daughter? It should return sister.

This is what my graph looks like. There are 8 relations and 2 labels (Male or Female).

My graph

How can I achieve this without specifying any node? For example if I want to know the relation between me and my father's daughter.

I want to do something like MATCH () -[:Father]->()-[:Daughter]->() and relationship between start and end node.

I'm sure this query is absolutely wrong but I hope you get an idea what I'm trying to achieve here.

Upvotes: 1

Views: 350

Answers (1)

FylmTM
FylmTM

Reputation: 2007

So,

At first - you should always specify some nodes.

Let's create some data:

CREATE (me:Person)
CREATE (mother:Person)
CREATE (daughter:Person)
CREATE (me)-[:Son]->(mother)
CREATE (me)<-[:Mother]-(mother)
CREATE (me)-[:Brother]->(daughter)
CREATE (me)<-[:Sister]-(daughter)
CREATE (mother)-[:Mother]->(daughter)
CREATE (mother)<-[:Daughter]-(daughter)

data


What's the relation between me and my mother's daughter? It should return sister.

Now we can find anwser to your question:

MATCH (me)<-[:Mother]-()<-[:Daughter]-(she)
MATCH (me)<-[r]-(she)
RETURN type(r)

Result:

Sister

What we have been done:

  • First match: specify starting path
  • Second match: ask for desired path
  • Return: get path results

For example I want to know what's the relation between me and my father's wife? It should return mother.

Here something like this should work:

MATCH (me)<-[:Father]-()<-[:Wife]-(she)
MATCH (me)<-[r]-(she)
RETURN type(r)

Result:

Mother

Upvotes: 5

Related Questions