Calculate metrics in recurse query

(Chenyuzhi) #1

Hi, I use recurse query to show users which points to their subordinates with an uid predicate named ~member_next_level_of . And I would like to count the number of subordinates (both direct and indirect) of every user in this query.

For example, userA has a subordinate named userB; userB has two subordinates named userC and userD, thus the result would be userA has 3 subordinates, userB has 2 subordinates, userC and userD have 0 subordinate.

Here is the query block:

	q as event(func: allofterms(member_id, "4114396")) @recurse(loop: false) { 
        a as ~member_next_level_of 


    var(func: uid(q,a)){
        count1 as math(1)
        ~member_next_level_of {
            count2 as count2: math(count1)  
                count3 as math(count2)
                    count4 as math(count3)
                sum_count4 as sum(val(count4))
            sum_count3 as sum(val(count3))
            sum_total3 as sum(val(sum_count4))
            total3 as math(sum_count3 + sum_total3)
        sum_count2 as sum(val(count2))
        sum_total as sum(val(total3))
        total as  math(sum_count2 + sum_total  + count1 -1)



Obviously, I had to organize the attribute structure manually to calculate the total number of subordinates of every user.

Can I use a better query method?

Does the community have a development plan to provide direct aggregation for in the future? Beause I have 9 levels for all users which is difficult to organize the query block.