1
votes

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.

1
You can't match a relation without any node, I mean if you want to know the relation between you and your father's daugther, you have to use the node that referees to you (or to your father's daughter). Else you will match every relationship names "Father" of "Daughter" - Supamiu
But every relation will return the same value, right? So not really a problem. - Pranav
I don't understand your question so ^^" - Supamiu
@Supamiu Is there a way to get all the () -[:Father]->()-[:Daughter]->() nodes that match this? - Pranav

1 Answers

5
votes

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