Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
05-25-2020 05:52 PM
I am using Neo4J to manage the family tree, I want to get 3 generations of a person and display it in the React Tree Graph library. The JSON structure I need will look like this:
{
"name": "Alex Statham",
"people": [
{
"name": "Jason Statham",
"people": [
{
"name": "Lyna Statham"
},
{
"name": "John Statham"
}
]
},
{
"name": "Will Statham",
"people": [
{
"name": "Michael Statham"
}
]
}
]
}
This is my family tree in Neo4J:
create (bn1:Person{maBN:1,name:"Anna William",address:"American"})
create (bn2:Person{maBN:2,name:"Jason Statham",address:"American"})
create (bn3:Person{maBN:3,name:"Lyna Statham",address:"American"})
create (bn4:Person{maBN:4,name:"John Statham",address:"American"})
create (bn5:Person{maBN:5,name:"Alex Statham",address:"American"})
create (bn6:Person{maBN:6,name:"Will Statham",address:"American"})
create (bn7:Person{maBN:7,name:"Michael Statham",address:"American"})
MATCH (a:Person),(b:Person),(c:Person),(d:Person)
WHERE a.name contains 'Jason' AND b.name contains 'Anna' and c.name contains 'John' and d.name contains 'Lyna'
CREATE (b)-[:PARENT_OF {type: 'natural'}]->(c)
CREATE (a)-[:PARENT_OF {type: 'natural'}]->(c)
CREATE (b)-[:PARENT_OF {type: 'natural'}]->(d)
CREATE (a)-[:PARENT_OF {type: 'natural'}]->(d)
RETURN a, b, c ,d
MATCH (a:Person),(b:Person)
WHERE a.name contains 'Jason' AND b.name contains 'Anna'
CREATE (a)-[:SPOUSE_OF {type:'marriage'}]->(b)
CREATE (b)-[:SPOUSE_OF {type:'marriage'}]->(a)
RETURN a, b
MATCH (a:Person),(b:Person)
WHERE a.name contains 'Alex' AND b.name contains 'Jason'
CREATE (a)-[:PARENT_OF {type:'natural'}]->(b)
RETURN a, b
MATCH (a:Person),(b:Person)
WHERE a.name contains 'Alex' AND b.name = 'Will Statham'
CREATE (a)-[:PARENT_OF {type:'adopted child'}]->(b)
RETURN a, b
MATCH (a:Person),(b:Person)
WHERE a.name='Will Statham' AND b.name contains 'Michael'
CREATE (a)-[:PARENT_OF {type:'step'}]->(b)
RETURN a, b
I tried several of query and procedure like these:
//Test 1 (It lacks a brother'child and of course it not return a JSON)
Match (otherssubchild)-[a:PARENT_OF]-(othersubchild)-[:PARENT_OF]-(otherchild)-[:PARENT_OF]-(parent)-[:PARENT_OF]-(b:Person)-[:PARENT_OF]-(children)-[:PARENT_OF]-(subchild)
where b.name contains 'Jason'
return parent,b,children,subchild,otherchild,othersubchild,otherssubchild
//Test 2 (It return a JSON but it not actually right and It wrong because start Node p1, p1 will always display like the oldest person)
MATCH (p1:Person {maBN:2})
CALL apoc.path.spanningTree(p1, {
sequence: '>Person,PARENT_OF,>Person,PARENT_OF,>Person',
maxLevel: 3
}) YIELD path
With collect(path) as R
Call apoc.convert.toTree(R) yield value
return value
//Test 3 (It looks good but is only true in the case of not indicating whose generation and in most cases is wrong.)
Match (a:Person)<-[:PARENT_OF]-(b:Person)<-[:PARENT_OF]-(c:Person)
WITH a,b,{name:c.name} as gen1
WITH a,{name:b.name, people:collect(gen1)} as gen2
WITH a,{name:a.name, people:collect(gen2)} as tree
return tree
But none of them is true. What do I have to do to get the JSON perfect for the library? Please someone help me!
Thanks a lot and sorry for my bad English!
09-15-2021 11:32 PM
check out the project I'm working on:
Neo4j Genealogy PlugIn
also described at this blog post
All the sessions of the conference are now available online