Neo4J node traversal cypher where clause for each node -


i've been playing neo4j geneology site , it's worked great!

i've run snag finding starting node isn't easy. looking through docs , posts online haven't seen hints @ maybe isn't possible.

what pass in list of genders , list follow specific path through nodes single node.

in context of family:

i want mother's father's mother's mother. have id start there , traverse 4 nodes mine.

so pseudo query be

select person (follow childof relationship) starting node me firstnode.gender == female   , secondnode.gender == male   , thirdnode.gender == female   , fourthnode.gender == female 

focusing on general solution:

match p = (me:person)-[:is_child_of*]->(ancestor:person) me.uuid = {uuid}   , length(p) = size({genders})   , extract(x in tail(nodes(p)) | x.gender) = {genders} return ancestor 

here's how works:

  1. match starting node id
  2. match variable-length paths going ancestor
  3. constrain length of path (i.e. number of relationships, same number of ancestors), can't parameterize length in query
  4. extract genders in path
    1. nodes(p) returns nodes in path, including starting node
    2. tail(nodes(p)) skips first element of list, i.e. starting node, have ancestors
    3. extract() extracts genders of ancestor nodes, i.e. transforms list of ancestor nodes genders
    4. the extracted list of genders can compared parameter
  5. if path matched, can return bound ancestor, end of path

however, don't think faster explicit solution, though performance remain comparable. on small test data (just 5 nodes), general solution 26 db accesses whereas specific solution 22, reported profile. further profiling needed on larger database compare performances:

profile match p = (me:person)-[:is_child_of*]->(ancestor:person) me.uuid = {uuid}   , length(p) = size({genders})   , extract(x in tail(nodes(p)) | x.gender) = {genders} return ancestor 

the general solution has advantage of being single query won't need parsed again cypher engine, whereas each generated query need parsed.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -