cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Non-symmetrical relationships

guido
Graph Buddy

I would greatly appreciate some advice on relationships.

Consider the following graph:

family.png

I want to avoid redundant (“sister of”) and symmetric relationships (“child of” alongside “parent of”), which works fine in a graph presentation.
However, the database I am building focusses on summarising all available information on specific persons, mostly in the form of tables (downloadable reports in NeoDash).
So, e.g. the family data sheet on Marie would look like this:

Marie
Daughter of               Lilly
Granddaughter of    Ann
Sister of                    Sophie
Mother of                  Karen
Cousin of                  Clara

So far I haven’t been able to find any instructions or suggestions on how to do this. Can anyone point me in the right direction?

1 ACCEPTED SOLUTION

Try this. It uses pattern comprehension to find each type of relations with a matching pattern to identify those relationships. The values returned a lists, incase there are multiple within the relationships. If you know there is only one in each category, you can append '[0]' to the end of each list

match(n:Person{name:'Marie'})
return {
    `daughter of`: [(n)<-[:PARENT_OF]-(m:Person)|m.name],
    `Granddaugher of`: [(n)<-[:PARENT_OF*2]-(m:Person)|m.name],
    `Sister of`: [(n)<-[:PARENT_OF]-(:Person)-[:PARENT_OF]->(m)|m.name],
    `Mother of`: [(n)-[:PARENT_OF]->(m)|m.name],
    `Cousing of`: [(n)<-[:PARENT_OF*2..]-(:Person)-[:PARENT_OF*2..]->(m)|m.name]
}

Screen Shot 2022-10-01 at 11.03.11 AM.png 

{
  "Mother of": [
    "Karen"
  ],
  "daughter of": [
    "Lilly"
  ],
  "Cousing of": [
    "Clara"
  ],
  "Granddaugher of": [
    "Ann"
  ],
  "Sister of": [
    "Sophie"
  ]
}

 The values returned a lists, in case there are multiple entities within a relationship. If you know there is only one in each category and you want to simplify the output, then try this:

match(n:Person{name:'Marie'})
return {
    `daughter of`: [(n)<-[:PARENT_OF]-(m:Person)|m.name][0],
    `Granddaugher of`: [(n)<-[:PARENT_OF*2]-(m:Person)|m.name][0],
    `Sister of`: [(n)<-[:PARENT_OF]-(:Person)-[:PARENT_OF]->(m)|m.name][0],
    `Mother of`: [(n)-[:PARENT_OF]->(m)|m.name][0],
    `Cousing of`: [(n)<-[:PARENT_OF*2..]-(:Person)-[:PARENT_OF*2..]->(m)|m.name][0]
}

 

{
  "Mother of": "Karen",
  "daughter of": "Lilly",
  "Cousing of": "Clara",
  "Granddaugher of": "Ann",
  "Sister of": "Sophie"
}

View solution in original post

2 REPLIES 2

Try this. It uses pattern comprehension to find each type of relations with a matching pattern to identify those relationships. The values returned a lists, incase there are multiple within the relationships. If you know there is only one in each category, you can append '[0]' to the end of each list

match(n:Person{name:'Marie'})
return {
    `daughter of`: [(n)<-[:PARENT_OF]-(m:Person)|m.name],
    `Granddaugher of`: [(n)<-[:PARENT_OF*2]-(m:Person)|m.name],
    `Sister of`: [(n)<-[:PARENT_OF]-(:Person)-[:PARENT_OF]->(m)|m.name],
    `Mother of`: [(n)-[:PARENT_OF]->(m)|m.name],
    `Cousing of`: [(n)<-[:PARENT_OF*2..]-(:Person)-[:PARENT_OF*2..]->(m)|m.name]
}

Screen Shot 2022-10-01 at 11.03.11 AM.png 

{
  "Mother of": [
    "Karen"
  ],
  "daughter of": [
    "Lilly"
  ],
  "Cousing of": [
    "Clara"
  ],
  "Granddaugher of": [
    "Ann"
  ],
  "Sister of": [
    "Sophie"
  ]
}

 The values returned a lists, in case there are multiple entities within a relationship. If you know there is only one in each category and you want to simplify the output, then try this:

match(n:Person{name:'Marie'})
return {
    `daughter of`: [(n)<-[:PARENT_OF]-(m:Person)|m.name][0],
    `Granddaugher of`: [(n)<-[:PARENT_OF*2]-(m:Person)|m.name][0],
    `Sister of`: [(n)<-[:PARENT_OF]-(:Person)-[:PARENT_OF]->(m)|m.name][0],
    `Mother of`: [(n)-[:PARENT_OF]->(m)|m.name][0],
    `Cousing of`: [(n)<-[:PARENT_OF*2..]-(:Person)-[:PARENT_OF*2..]->(m)|m.name][0]
}

 

{
  "Mother of": "Karen",
  "daughter of": "Lilly",
  "Cousing of": "Clara",
  "Granddaugher of": "Ann",
  "Sister of": "Sophie"
}

Hi,

tested in and it worked. Thanks (again)!

Guido