1

I have a graph on Azure CosmosDB which involves company hierarchies where a Top Parent exists and there are child companies like

CompanyX(Top Parent) -> CompanyY(Child of X + Parent of A) -> CompanyA(Child of Y + Parent of B,C,D)  
                                                                 |----> CompanyB(Child of A)  
                                                                 |----> CompanyC(Child of A)  
                                                                 |----> CompanyD(Child of A)  

If I'm asked about CompanyA, my goal is to find out :

  1. some properties of CompanyA.
  2. some properties of CompanyA's parent i.e properties of CompanyY.
  3. some properties of CompanyA's children i.e properties of Companies B,C and D.
  4. some properties of CompanyA's Top Parent i.e properties of CompanyX.

So far, from reading the official Tinkerpop documentation and from reading sections of Practical Gremlin, I've managed to do the above using two queries like

first_query = "g.V().has('name', 'CompanyA').project('current', 'parent', 'children').by(__.map(valueMap(true).unfold().group().by(keys).by(select(values).limit(local,1)))).by(__.in().valueMap('name','company_id').fold()).by(out().valueMap('name','company_id').fold())"

top_parent_vertex_id = first_query[0]['current']['top_parent_number']

second_query = "g.V(top_parent_vertex_id).project('top_parent').by(__.map(valueMap('name','company_id').unfold().group().by(keys).by(select(values).limit(local,1))))"

Is there a way to do this more efficiently, possibly in a single query, by using the property value from my traversal to find the top parent too?

maybe like

first_query = "g.V().has('name', 'CompanyA').project('current', 'parent', 'children', 'top_parent').by(__.map(valueMap(true).unfold().group().by(keys).by(select(values).limit(local,1)))).by(__.in().valueMap('name','company_id').fold()).by(out().valueMap('name','company_id').fold()).by(**V(<property_value_from_CompanyA>)**.valueMap().fold())"

I know the above query is wrong. I'd like to understand to how to achieve my goal.

0 Answers0