gpt4 book ai didi

graphql - 如何分组和计算dgraph

转载 作者:行者123 更新时间:2023-12-04 04:23:59 27 4
gpt4 key购买 nike

所以对于这个PersonCompany 示例:

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

最佳答案

检查这是否适合您。 (让我们用我的示例来弄清楚这个问题)

{
var(func: anyofterms(name, "Alexei Jack Jose Zoe Ivy")) @groupby(works_for) {
a as count(uid)
}

q(func: uid(a), orderdesc: val(a)) {
name
total_workers : val(a)
workHere: ~works_for @groupby(name){
count(uid)
}
}
}

结果

此查询并不完美,因为它利用了 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
}
]
}
]
}
]
}
}

使用此数据集作为引用:https://tour.dgraph.io/master/schema/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 .
}
}

关于graphql - 如何分组和计算dgraph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58311554/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com