Query who knows people from more than two countries

What I want to do

here’s the schema:

type Persion {
  name
  friends
  nationality
}

type Country {
  name
}

and sample data:

_:a <dgraph.type> "Persion" .
_:a <name> "A" .
_:b <dgraph.type> "Persion" .
_:b <name> "B" .
_:c <dgraph.type> "Persion" .
_:c <name> "C" .
_:y <dgraph.type> "Country" .
_:y <name> "Y" .
_:z <dgraph.type> "Country" .
_:z <name> "Z" .

_:a <friends> _:b .
_:a <friends> _:c .
_:b <friends> _:c .
_:b <friends> _:a .

_:a <nationality> _:z .
_:b <nationality> _:y .
_:c <nationality> _:y .

What I did

Query who knows people from more than two countries.

I’ve tried many queries but none get work, here’s is one of my queries:

{
  q(func: eq(dgraph.type, "Persion")) @filter(ge(cs, 2)  @cascade{
    name
    cs as count(friends.nationality)

    friends  {
        name
        nationality {
           name
        }
    }
    
  }
}

Expected results:

{
   "name": "B",
   "friends":[
       {
           "name": "A",
           "nationality":  {
               "name": "Z",
           }
       },
       {
           "name": "C",
           "nationality":  {
               "name": "Y",
           }
       }
   ]
   
}

What did I missunderstanding? Any suggestions?

Dgraph version : v20.11.3

1 Like

不好搞啊 感觉已经临门一脚了 还是不行

{
  q(func: eq(dgraph.type, "Persion"))@filter(ge(count(friends), 2)) @cascade{
    name
    xx as uid

    yy as count(friends)
    friends @groupby(nationality){
      zz as count(uid)
    }
    cc as math(zz+yy)
  }
  qq(func: uid(zz)){
    val(zz)
  }
  qqq(func: uid(xx), orderdesc: val(cc)){
    val(cc)
    uid
    name
      friends {
      name
      nationality{
        name
      }
    }
  }
}
1 Like

Thank you for trying!
This is a requirement for statistical subgraphs, but it does not seem to support it at present.

{
  q(func: eq(dgraph.type, "Persion"))  @cascade{
    name
    friends  {
        name
        nationality (offset: 2) {
           name
        }
    }
    
  }
}