Get common parent node

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.

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?

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)

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.

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

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.

@MichelDiz:
If my question is little different.
How can we get common followers
A,B follows C (A,B → C)
A,X follows D (A,X → D)

Common followers for C and D is A
How can i get this?
C–>1234
D–>5678

Example
The query is
{
var(func : eq(Id, “1234”)) {
user1_follows as follow{
uid
}
}

var(func : eq(Id, “5678”)) {
user2_follows as follow{
uid
}
}

common_followers(func : uid(user1_follows))
@filter (uid(user2_follows)){
Id
}
}

But the latency is little high as our data is too high.
is there any better way to write the query than the above?

Can anyone help me in optimizing the above query?

Sorry the delay. Well, I think there’s no other way. You may have to improve the resources of your instances to get better results. In terms of query language there’s no other way I can think of.

Cheers.

maybe, it is graph analysis,not graph search. Dgraph is not good at graph analysis

It is not the case of graph analysis.
we do have requirement of common followers for particular set of users, which we are trying to achieve with dgraph.

If the followers are too many, calc the common followers in realtime is too expensive, may you can pre-calc the common followers use offline analysis like spark graphx and then serve as an online service.

:smiley: just try another way

{
  q(func : eq(Id, “1234”)) @cascade{//  a 
   a: uid
   follow{  // follow a
   common: uid 
   ~follow  @filter(eq(Id, “5678”)){ // b
    b:uid  // flollow b
   }
   }
}