简体   繁体   中英

Neo4J/Cypher : variable length of path pattern

I model a genealogy on a graph in Neo4J inspired by GEDCOM file.

My nodes and relations are :

Individual <-[CHILD]- Family
Family -[HUSBAND]-> Individual
Family -[WIFE]-> Individual

I don't model the family as a relation because i can have multiple event attached to it (Engagement,Marriage,Annulment,Divorce,...) :

Family -[OCCUR]-> FamilyEvent{type,subtype,date,place,note}

I can get the father and mother of a person with this cypher query :

MATCH (i:Individual {nickname:'Louis XVI'})
        <-[r:CHILD]-
    (m:Family)
        -[r2:HUSBAND|WIFE]->
    (h:Individual) 
    return i,r,m,r2,h

Or the child of a person :

MATCH (i:Individual {nickname:'le Pieux ou le Débonnaire'})
        <-[r:HUSBAND]-
    (m:Family)
        -[r2:CHILD]->
    (h:Individual) 
    return i,r,m,r2,h

But how can i get all the ascendants or descendants of a person ?
(In other way, how can i repeat the pattern between individual or apply the same pattern to individual i get on each level?)

You can use this query

match (n:individual{id:###})<-[:child*..9]-(n) return n,m

to create ancestor tree or the arrow pointing opposite for descendants.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM