Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic traversal of a directed tree with Neo4J

I modelled a directed tree structure using the graph database Neo4J. So I have something like this: http://ouwarovite.net/YAPC/220px-Binary_tree.svg.png (not mandatory binary)

Users of my database can add child nodes of existing nodes at will, so the height of the tree and the degree of the single nodes is unknown.

Now, I want to query my tree like this: Starting with node x, give me all leaves that are descendants of leave x.

Is this kind of query performable with Gremlin or Cypher and if so, how to do this achieving the maximum of performance? (I haven't found a possibility to perform queries on 'generic' trees because you alway have to specify a maximum depth)

I know, that it's possible with the REST / JSON framework and the JAVA API like this:

POST /db/data/node/51/traverse/node 
{ 
"return_filter" : 
    {
    "body" : "position.endNode().hasProperty('leave')" ,
    "language" : "javascript" 
     }, 
"relationships" : [ { "type" : "_default", "direction" : "out" } ] , 
"prune_evaluator" : { "name" : "none" , "language" : "builtin" }
}

(my leaves have the property 'leave', my edges have no type -> so _default)

Is there a simpler / better way to do this maybe with a better performance?

like image 613
user983962 Avatar asked Oct 07 '11 12:10

user983962


1 Answers

Cypher could look like that:

start root=node({rootId}) 
match root-[*]->child
where child.leave
return child

rootId being a parameter to be passed in.

like image 77
Michael Hunger Avatar answered Sep 19 '22 23:09

Michael Hunger