Friend recommendation get mutual friends

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

Something like this should work for you.

{
 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(fr,me)) { # Remove the user and his friends
  name
  # Fetch the friends of recommendations and intersect with the friends of uid(0x1) to get list of mutual friends.
  friend @filter(uid(fr)) {
    uid
    name
  }
 }
}

Also, there was a some problem in your data-set which I have rectified and shared the updated data-set below.


{
    "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": "0x3"
                }
            ]
        },
        {
            "name": "E",
            "uid": "0x5",
            "friend": [
                {
                    "name": "A",
                    "uid": "0x1"
                },
                {
                    "name": "F",
                    "uid": "0x6"
                }
            ]
        },
        {
            "name": "F",
            "uid": "0x6",
            "friend": [
                {
                    "name": "E",
                    "uid": "0x5"
                }
            ]
        }
    ]
}

Note - How node D was connected to itself i.e. to a node with name C but uid as 0x4 which caused inconsistencies in the query result.

Thank you very much, it works for me

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.