Hi, it’s surprisingly quite difficult to do graph-y stuff in dgraph. I’m working on adding some tips and tricks that I found to be quite useful for doing graph stuff (like indegrees, outdegrees, etc)
Here’s what I have so far for your problem (I had to index gid as terms, and add @reverse to impress):
{
{
var(func: allofterms(gid, "test1")){
# A as uid
B as impress{uid}
}
var(func: allofterms(gid, "test2")){
# C as uid
D as impress{uid}
}
var(func: uid(B,D) ){
U as count(userid)
}
var(func:uid(B)) @filter(uid(D)){
I as count(userid)
}
var(){II as sum(val(I))}
var(){UU as sum(val(U))}
q(){
aa : math(II*1.0/UU*1.0)
intersection : sum(val(I))
union: sum(val(U))
}
}
I get the following values as a result:
"data": {
"q": [
{
"aa": 0.5
},
{
"intersection": 3
},
{
"union": 6
}
]
}
The *1.0 is a trick to promote the count into floats such that division will be a float division