Saturday 15 September 2012

graph databases - How to realize a nested tree in Neo4j? -



graph databases - How to realize a nested tree in Neo4j? -

i getting started neo4j , graph databases , wondering if nested hierarchical trees utilize case neo4j. mutual illustration nested set of comments. 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 understand it, article , comments each nodes. , each comment have parent-child relationship. getting direct comments on article (1 , 5) easy. how retrieving whole set?

please excuse utilize of layman terms. figured improve way trying utilize appropriate words while confusing everyone.

well, because trees graphs using graph database store tree seems exclusively appropriate me. perform best depend on info access patterns, trees specialization of graphs.

yes, in case "elements in tree" nodes, , "nesting" relationships. here's how might mock example:

create (root:article {label: "article"}), (c1:comment {label: "comment 1"}), (c1a:comment {label: "comment 2 on comment 1"}), (c1b:comment {label: "comment 3 on comment 1"}), (c1b1:comment {label: "comment 4 on comment 3"}), (c2:comment {label: "comment 5 on article"}), (root)<-[:reply]-(c1), (c1)<-[:reply]-(c1a), (c1)<-[:reply]-(c1b), (c1b)<-[:reply]-(c1b1), (root)<-[:reply]-(c2);

this creates bunch of nodes , relationships, mimicking construction provided. here i've chosen utilize :reply connect things.

now, "getting everything" means traversing of :reply relationships created:

match p=(a:article {label: "article"})<-[:reply*1..]-(otherthing) nodes(p) nodes homecoming nodes[length(nodes)-2] inresponseto, nodes[length(nodes)-1] commentorreply;

what query traverse number of :reply links (starting root "article"). looks @ nodes in path, , returns lastly item (commentorreply) , in response (the sec lastly item).

the result looks this:

+-------------------------------------------------------------------------------------+ | inresponseto | commentorreply | +-------------------------------------------------------------------------------------+ | node[18]{label:"article"} | node[19]{label:"comment 1"} | | node[19]{label:"comment 1"} | node[20]{label:"comment 2 on comment 1"} | | node[19]{label:"comment 1"} | node[21]{label:"comment 3 on comment 1"} | | node[21]{label:"comment 3 on comment 1"} | node[22]{label:"comment 4 on comment 3"} | | node[18]{label:"article"} | node[23]{label:"comment 5 on article"} | +-------------------------------------------------------------------------------------+

so that's how traverse entire tree.

edit - it's worth, "variable length path matching", in query above bit: <-[:reply*1..]- me 1 of top 3 selling points graph database. it's exactly graph dbs create easy, of other alternatives relational create tortuously painful exercise. if need sort of thing (like here, assembling entire tree) claim argues graph db because you're using in area of fundamental strength.

neo4j graph-databases

No comments:

Post a Comment