Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
10-01-2022 12:26 AM
I would greatly appreciate some advice on relationships.
Consider the following graph:
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?
Solved! Go to Solution.
10-01-2022 08:15 AM
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]
}
{
"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"
}
10-01-2022 08:15 AM
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]
}
{
"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"
}
10-04-2022 01:48 AM
Hi,
tested in and it worked. Thanks (again)!
Guido
All the sessions of the conference are now available online