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:
- match starting node id
- match variable-length paths going ancestor
- constrain length of path (i.e. number of relationships, same number of ancestors), can't parameterize length in query
- extract genders in path
nodes(p)
returns nodes in path, including starting nodetail(nodes(p))
skips first element of list, i.e. starting node, have ancestorsextract()
extracts genders of ancestor nodes, i.e. transforms list of ancestor nodes genders- the extracted list of genders can compared parameter
- 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
Post a Comment