It very useful to recommend friend by the query:
{
me as var(func: uid(<userid>)) {
sc as math(1) # Give a score of 1 to the user
fr as friend {
friend {
fscore as math(sc) # This will be number of common friends
}
}
}
# Get top ten people with highest score
TopRecommendations(func: uid(fscore), orderdesc: val(fscore), first: 10) @filter(not uid(me, fr)) { # Remove the user and his friends
name
}
}
A <friend> B<friend> D
A <friend> C<friend> D
A <friend> E<friend> F
We can use this query to find the users A that will be recommended(D and F)
Further, Can we find the mutual friends with the recommended in this query?
example:
{
"set": [
{
"name": "A",
"uid": "0x1",
"friend": [
{
"name": "B",
"uid": "0x2"
},
{
"name": "C",
"uid": "0x3"
},
{
"name": "E",
"uid": "0x5"
}
]
},
{
"name": "B",
"uid": "0x2",
"friend": [
{
"name": "D",
"uid": "0x4"
}
]
},
{
"name": "C",
"uid": "0x3",
"friend": [
{
"name": "D",
"uid": "0x4"
}
]
},
{
"name": "D",
"uid": "0x4",
"friend": [
{
"name": "B",
"uid": "0x2"
},
{
"name": "C",
"uid": "0x4"
}
]
},
{
"name": "E",
"uid": "0x5",
"friend": [
{
"name": "A",
"uid": "0x1"
},
{
"name": "F",
"uid": "0x6"
}
]
},
{
"name": "F",
"uid": "0x6",
"friend": [
{
"name": "E",
"uid": "0x5"
}
]
}
]
}
query:
{
me as var(func: uid(0x1)) {
sc as math(1) # Give a score of 1 to the user
fr as friend {
friend {
fscore as math(sc) # This will be number of common friends
}
}
}
# Get top ten people with highest score
TopRecommendations(func: uid(fscore), orderdesc: val(fscore), first: 10) @filter(not uid(me, fr)) { # Remove the user and his friends
name
}
}
The recommendations result is:
{
"data": {
"TopRecommendations": [
{
"name": "D",
"fscore": 2
},
{
"name": "F",
"fscore": 1
}
]
...
In fact, I want to find a way not only get the D、F information but also the mutual friends between
A and D
the result may be like this:
{
"data": {
"TopRecommendations": [
{
"name": "D",
"fscore": 2,
"mutual": [
{
"name": "B",
"uid": "0x2"
},
{
"name": "C",
"uid": "0x3"
}
]
},
{
"name": "F",
"fscore": 1,
"mutual": [
{
"name": "E",
"uid": "0x5"
}
]
}
]
}
}
D will recommend to A, A、D have the same friends B、C
F will recommend to A, A、F have the same friend E