Count filter in internal block

Hi,

I want to perform a count filter inside an inner block

Taking as reference the following Type

type Person {
     name
     age
     friend
}

and relationships between Person type nodes

I have the following query

{
   lots_of_friends (func: gt (count (friend), 1)) @normalize @cascade {
     name: name @.
    friends: count (friend)
     friend @filter (gt (age, 30)) {
       ge_30: count (uid)
         name@.
     }
   }
}

searching people who have more than one friend and those friends are over 30 years old.

which returns the following:

"data": {
     "lots_of_friends": [
       {
         "name": "Amit",
         "friends": 3,
         "ge_30": 2
       },
       {
         "name": "Michael",
         "friends": 5,
         "ge_30": 3
       },
       {
         "name": "Sang Hyun",
         "friends": 3,
         "ge_30": 1
       }
     ]
   },

However, I want to make a filter that returns only the nodes that have more than 2 friends of 30 years,
so it should only return to “Michael” not Amit, who has 2, nor “Sang Hyun”, who only has 1.

I don’t know if I have to use variables or it is easier than I think

Thanks

Do you mean friends of friends?
Try this

{
   lots_of_friends (func: gt (count (friend), 1)) @normalize @cascade {
     name: name @.
    friends: count (friend)
     friend @filter (gt (age, 30))  @cascade {
       ge_30: count (uid)
         name@.
         friend @filter (gt (age, 30)) { UID }
     }
   }
}

Not really, I want to set a filter that only when the alias ge_30 is equal to or greater than 3 will show the root node.

The result should be:

“data”: {
“lots_of_friends”: [
{
“name”: “Michael”,
“friends”: 5,
“ge_30”: 3
}
]
},

instead of:

“data”: {
“lots_of_friends”: [
{
“name”: “Amit”,
“friends”: 3,
“ge_30”: 2
},
{
“name”: “Michael”,
“friends”: 5,
“ge_30”: 3
},
{
“name”: “Sang Hyun”,
“friends”: 3,
“ge_30”: 1
}
]
},

Thanks

Maybe something like this

{
   F as var(func: gt (count (friend), 1)) @cascade {
     name @.
     friend @filter(gt (age, 30)) {
       GE as count (uid)
         name@.
         friend @filter (gt (age, 30)) { UID }
     }
   }
      lots_of_friends(func: uid(F)) @normalize {
        name @.
        friend @filter(ge (val(GE), 3)) {
          ge_30: count (uid)
          name@.
     }
   }
}

What dataset are you using?

For these examples I am using the examples from these two pages of dgraph:

https://dgraph.io/tour/intro/3/#/
https://dgraph.io/tour/intro/4/

Thanks for your time MichelDiz,
I’ve been running queries similar to the one you suggest. The problem is that GE as count (uid) does not seem to store anything.

I think I have a query that works.
The key is to somehow store the friends who are over 30 years old and for this I use math (1) to assign 1 to the variable gt_30 for the uid n. At the end, the values of that variable are added, which will be used to make the filter.

{
F as var (func: gt (count (friend), 1)) @cascade {
friend @filter (gt (age, 30)) {
gt_30 as math(1)
}
sum_myscore as sum(val(gt_30))

}

lots_of_friends(func: uid(F)) @filter (ge(val(sum_myscore),3)) @normalize {
  name: name @.
  total_friends: count(friend)      
  total_friends_age_gt_30: val(sum_myscore)
}

}