Graphql 如何按分组并计算dgraph

Graphql 如何按分组并计算dgraph,graphql,dgraph,Graphql,Dgraph,因此,对于这个人,公司示例: type Person { name: string work_for: [Company] } type Company { name: string work_for: [Person] } work_for: [uid] @reverse @count . name: string @index(fulltext, term) @count . 查询将找到什么:所有拥有“John James Sam”的公司,每个公司的这些

因此,对于这个
公司
示例:

type Person {
    name: string
    work_for: [Company]
}

type Company {
    name: string
    work_for: [Person]
}

work_for: [uid] @reverse @count .
name: string @index(fulltext, term) @count .
查询将找到什么:
所有拥有“John James Sam”的公司,每个公司的这些名字的总数都将被构造出来?我试过:

  q(func: anyofterms(hashed, "Jame John Sam")){
        uid
    @groupby work_for{
      name
      count(uid)
    }
  }
}```

but that gives the person and the companies they worked for plus the count which is not the company and the number of people with those names that worked for it. Thanks in advance

检查这是否适合你。(让我们用我的样本来了解这一点)

结果 此查询并不完美,因为它利用了GroupBy中未实现的功能。但是,您可以使用此功能,也可以期望GroupBy功能得到改进

{
  "data": {
    "q": [
      {
        "name": "CompanyABC",
        "total_workers": 6,
        "workHere": [
          {
            "@groupby": [
              {
                "name": "Ivy",
                "count": 2
              },
              {
                "name": "Jack",
                "count": 2
              },
              {
                "name": "Zoe",
                "count": 2
              }
            ]
          }
        ]
      },
      {
        "name": "The other company",
        "total_workers": 4,
        "workHere": [
          {
            "@groupby": [
              {
                "name": "Alexei",
                "count": 2
              },
              {
                "name": "Jose",
                "count": 2
              }
            ]
          }
        ]
      }
    ]
  }
}
使用此数据集作为参考:

我编辑了它,它看起来像这样:

{
  set {
    _:company1 <name> "CompanyABC" .
    _:company1 <dgraph.type> "Company" .
    _:company2 <name> "The other company" .
    _:company2 <dgraph.type> "Company" .

    _:company1 <industry> "Machinery" .

    _:company2 <industry> "High Tech" .

    _:jack <works_for> _:company1 .
    _:jack <dgraph.type> "Person" .

    _:ivy <works_for> _:company1 .
    _:ivy <dgraph.type> "Person" .

    _:zoe <works_for> _:company1 .
    _:zoe <dgraph.type> "Person" .

    _:jack <name> "Jack" .
    _:ivy <name> "Ivy" .
    _:zoe <name> "Zoe" .
    _:jose <name> "Jose" .
    _:alexei <name> "Alexei" .

    #duplicated

    _:jack2 <name> "Jack" .
        _:jack2 <works_for> _:company1 .
    _:jack2 <dgraph.type> "Person" .

    _:ivy2 <name> "Ivy" .
        _:ivy2 <works_for> _:company1 .
    _:ivy2 <dgraph.type> "Person" .

    _:zoe2 <name> "Zoe" .
        _:zoe2 <works_for> _:company1 .
    _:zoe2 <dgraph.type> "Person" .

    _:jose2 <name> "Jose" .
        _:jose2 <works_for> _:company2 .
    _:jose2 <dgraph.type> "Person" .

    _:alexei2 <name> "Alexei" .
        _:alexei2 <works_for> _:company2 .
    _:alexei2 <dgraph.type> "Person" .

        #duplicated end

    _:jose <works_for> _:company2 .
    _:jose <dgraph.type> "Person" .
    _:alexei <works_for> _:company2 .
    _:alexei <dgraph.type> "Person" .

    _:ivy <boss_of> _:jack .

    _:alexei <boss_of> _:jose .
  }
}
{
设置{
_:company1“CompanyABC”。
_:company1“Company”。
_:company2“另一家公司”。
_:company2“Company”。
_:公司1“机械”。
_:公司2“高科技”。
_:杰克:公司1。
_:杰克“人”。
_:常春藤:公司1。
_:常春藤“人”。
_:佐伊:公司1。
_:佐伊“人”。
_:杰克“杰克”。
_:常春藤“常春藤”。
_:佐伊“佐伊”。
_:何塞“何塞”。
_:alexei“alexei”。
#复制
_:jack2“Jack”。
_:jack2 u1:company1。
_:jack2“人”。
_:ivy2“常春藤”。
_:ivy2 u1:公司1。
_:ivy2“人”。
_:zoe2“Zoe”。
_:zoe2 u1:公司1。
_:zoe2“人”。
_:jose2“Jose”。
_:jose2 u2:company2。
_:jose2“人”。
_:Alexei 2“Alexei”。
_:alexei2 u2;:公司2。
_:alexei2“人”。
#重复端
_:何塞:公司2。
_:何塞“人”。
_:亚历克斯:公司2。
_:阿列克谢“人”。
_:常春藤:杰克。
_:阿列克谢:何塞。
}
}
{
  set {
    _:company1 <name> "CompanyABC" .
    _:company1 <dgraph.type> "Company" .
    _:company2 <name> "The other company" .
    _:company2 <dgraph.type> "Company" .

    _:company1 <industry> "Machinery" .

    _:company2 <industry> "High Tech" .

    _:jack <works_for> _:company1 .
    _:jack <dgraph.type> "Person" .

    _:ivy <works_for> _:company1 .
    _:ivy <dgraph.type> "Person" .

    _:zoe <works_for> _:company1 .
    _:zoe <dgraph.type> "Person" .

    _:jack <name> "Jack" .
    _:ivy <name> "Ivy" .
    _:zoe <name> "Zoe" .
    _:jose <name> "Jose" .
    _:alexei <name> "Alexei" .

    #duplicated

    _:jack2 <name> "Jack" .
        _:jack2 <works_for> _:company1 .
    _:jack2 <dgraph.type> "Person" .

    _:ivy2 <name> "Ivy" .
        _:ivy2 <works_for> _:company1 .
    _:ivy2 <dgraph.type> "Person" .

    _:zoe2 <name> "Zoe" .
        _:zoe2 <works_for> _:company1 .
    _:zoe2 <dgraph.type> "Person" .

    _:jose2 <name> "Jose" .
        _:jose2 <works_for> _:company2 .
    _:jose2 <dgraph.type> "Person" .

    _:alexei2 <name> "Alexei" .
        _:alexei2 <works_for> _:company2 .
    _:alexei2 <dgraph.type> "Person" .

        #duplicated end

    _:jose <works_for> _:company2 .
    _:jose <dgraph.type> "Person" .
    _:alexei <works_for> _:company2 .
    _:alexei <dgraph.type> "Person" .

    _:ivy <boss_of> _:jack .

    _:alexei <boss_of> _:jose .
  }
}