OrientDB query for hierarchical data -


orientdb server v2.0.10 , trying come query following scenario.

i have 2 hierarchies: a->b->c , d->e->f number of nodes in hierarchy can change. node in 1st hierarchy can connected other hierarchy using relation 'assigned'. want parent node of 2nd hierarchy if there incoming edge of node in 2nd hierarchy 1st.

for example, have car-child->engine-child->piston , country-child->state-child->city , relationship made_in relates car or engine or piston either country or state or city if there relation either of country or state or city, country should returned. example, engine1-made_in->berlin, return germany. sorry such toyish example. hope clear.

thanks.

you should consider reading chapter "traversing" - should missing link answer question. can find here: http://orientdb.com/docs/last/sql-traverse.html

basically, if think of graph family tree, want achieve 3 things:

  • find children, grand-children, grand-grand-children (and on) tree 1 given family member (=hierarchy1)

  • find have relations members of family tree (=assigned)

  • show me who's on top of tree (=hierarchy2)

one of possible solutions should little this:

since want end on top of hierarchy2, have start on other side, i.e. hierarchy1.

  1. get hierarchy1 (top-to-bottom)

     traverse out("child") car 
  2. choose relations

    select out("made_in) ([1]) 
  3. and those, go bottom-to-top

    traverse in("child") ([2]) 
  4. who's on top?

    select ([3]) @class="country" 

combined 1 sql, looks ugly this:

    select (        traverse in("child") (           select out("made_in") (              traverse out("child") car           )        )     ) @class="country" 

you replace car @rid in hierarchy1 list of countries or part of made in.

there might better solutions sure. @ least 1 should work, hope help.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -