I am just getting started with Neo4j and Graph Databases and was wondering if nested hierarchical trees are a good use case for Neo4j. A common example would be a nested set of comments. For example:
- Article
- Comment 1 on Article
- Comment 2 on Comment 1
- Comment 3 on Comment 1
- Comment 4 on Comment 3
- Comment 5 on Article
As I understand it, the article and the comments would each be nodes. And each comment would have a parent-child relationship. Getting all direct comments on the article (1 and 5) would be easy. But how about retrieving the whole set?
Please excuse the use of layman terms. I figured it is better this way then trying to use the appropriate words while confusing everyone.
If the nested trees are entirely directed graphs and have no cycles (i.e., directed acyclical graph=DAG), then you might consider transitive closure methods in a relational database. These allow very quick queries thru multiple nesting levels & finds of multiple intersections. They have the n-squared issue, so there are lots of rows, but with bigint indexes the queries run fast.
Well, because trees actually are graphs using a graph database to store a tree seems entirely appropriate to me. What will perform the best will depend on your data access patterns, but basically trees are just a specialization of graphs.
Yes, in your case the "elements in the tree" would be nodes, and the "nesting" would be relationships. So here's how you might mock up your example:
This just creates a bunch of nodes and relationships, mimicking the structure you provided. Here I've chosen to always use
:reply
to connect things.Now, "getting everything" just means traversing all of the
:reply
relationships we created:What this query does is traverse any number of
:reply
links (starting from the root "Article"). Then it only looks at the nodes in that path, and returns the last item (CommentOrReply
) and what it was in response to (the second last item).The result looks like this:
So that's how you traverse an entire tree.
EDIT - for what it's worth, "variable length path matching", which in the query above was just this bit:
<-[:reply*1..]-
is for me one of the top 3 selling points for a graph database. It's precisely that which graph DBs make really easy, that most of your other alternatives like relational make into a tortuously painful exercise. So if you need to do that sort of thing (like here, assembling an entire tree) I would claim that argues for a graph DB because you're using it in its area of fundamental strength.