Get common parent node


(Sascha Andres) #1

I want to select the first common boss for two employees in draph.

My model is simple:

name: string boss_of: uids

Lets assume the following data where each arrow denotes the boss_of edge:

A -> B
A -> C
B -> D
C -> E
E -> F
E -> G

So, given F And D the query should return A, for F and G the result is obviously E.

I tried using allofterms but found no solution as there may be a different number of nodes between the co-workers and their common boss. Is it possible at all to formulate such a query?

I am trying to explore dgraph (or graph databases at all), so maybe I am just overseeing something.


(Michel Conrado) #2

you mean B and C right?

Well, what is the context of what you wanna to do?

Should I assume you already have the employees UIDs and want to know if they have bosses in common?

Or should I assume that you want to find out how many employees have a specific boss in common?


(Sascha Andres) #3

I mean F and D. It just happens that A is also the boss of B and C

A -> B -> D
A -> C -> E -> F

A special case is when E and F is given. Then optimally the answer is C.

And regarding the uids: Yes, I already have the uids or can query them (maybe through a sub query? But that is probably another question)


(Michel Conrado) #4

Okay,

Your employees are 0x9c672b and 0x9c672c.

{
  var(func: uid(0x9c672c)) {
   A_BOSS as  ~boss_of # In this case you have to use reverse edges
  }
  var(func: uid(0x9c672b)) {
   B_BOSS as  ~boss_of 
  }
    
    query(func: uid(A_BOSS)) @filter(uid(B_BOSS)){
      uid
      boss_of {
        uid
      }
    }
}

This query above will return only if the bosses are the same. Otherwise will be “Your query did not return any results”.

Other case

If you have the boss UID and wanna check who has him as boss. You could use “uid_in”. e.g:

The boss UID is 0x679de1

{
  q(func: eq(section_company, "payment"))
   @filter(uid_in(~boss_of, 0x679de1)){ 
   # Now you can filter at root by the parent node via "boss_of" edge. 
   # In this case you have to use reverse edges
    name
    somethingElse
  }
}

See about reverse edges

This query above you gonna have all employees in payment sector but with common boss.


(Sascha Andres) #5

Hi,

that looks fine if both employees have the same boss. But what if the first boss in common is the CEO because A works in IT dept with the department head as boss and B works for accounting and is the CFO - the CEO is his boss. I don’t see how this is solved.

Best regards,
Sascha


(Michel Conrado) #6

Well the “CEO” is one level above “A” and “B”? if so, is the same logic above. If You wanna compare bosses at distant levels you query gets bigger. Because you need to solve the traverse way until there. Isn’t that hard, but needs work.