Seeking Assistance with Dgraph for User Recommendation System

Use Dgraph for my recommendation system

I am currently developing an online dating application with a growing user base of approximately 2000 users. I am using Dgraph for my recommendation system and I am seeking to improve the user matching algorithm.

Each user in my system has the following basic information:

type User  {
	id: string
	name: string
	dob: datetime
	location: Loc 	
	tagged: [Tag]
    total_dislike: int
	total_like: int
 }

I have successfully implemented a recommendation system that suggests a list of users with similar interests (tagged) to the current user. However, I am facing challenges in sorting the suggested users by distance and ensuring the age gap is not too large.

Here is my current Dgraph query to get the top 100 recommended users:

currentUser as var(func: eq(id, "356016f3-bebb")) {
    currentUserTags as tagged
}

var(func: type(User)){
    # Calculate similarity based on tags
    tagScore as count(tagged @filter(uid(currentUserTags)))

    # calculate popularity based on total like/dislike 
    popularityScore as math(wilsonScore(total_dislike,total_like))   

    # want to cal distanceScore here
  
    # want to cal ageScore here

    # final score
    finalScore as math(bumpScore + popularityScore)
}

user(func: uid(finalScore), first: 100, orderdesc: val(finalScore))
    @cascade
    @filter(
        NOT uid(currentUser)
    ) {
        user_id : id
        score : val(finalScore)
    }

Things you have tried

I have attempted to address these challenges in the following ways:

1. Sorting by Distance:

  • I have researched how to get or sort by distance in Dgraph, but it appears that Dgraph does not currently support this feature. (Add @distance for geo - #10 by MichelDiz)
  • I have tried using the @near directive, but it filters out many good users from my recommendation list.
  • My current workaround is to retrieve 100 users from Dgraph, then use PostgreSQL to calculate the distance and sort the list again.

2. Calculating Age Gap:

    currentUser as var(func: eq(id, "356016f3-bebb")) {
          currentUserTags as tagged
          currentDob as dob
          currentUserAge as math(since(currentDob)/(365*24*60*60))
    }

    var(func: type(User)){
          # Calculate similarity based on tags
          tagScore as count(tagged @filter(uid(currentUserTags)))
  
          # calculate popularity based on total like/dislike 
          popularityScore as math(wilsonScore(total_dislike,total_like))   

         # calculate  age gap               
          d as dob 
          age as math(since(d)/(365*24*60*60)) # age return 20.08
          ageDif as math(currentUserAge - age) # ageDif ALWAYS RETURN -20.08


          # final score, consider which factor is more important, then multiply it by 2
          finalScore as math(bumpScore + popularityScore + ageDif)
    }

However, the currentUserAge variable always returns 0. I have verified that the user with id “356016f3-bebb” has a valid dob value. When I run the following query:

{
    user(func: eq(id, "356016f3-bebb")) {
      d as dob
      age as math(since(d)/(365*24*60*60))
      age: val(age)
    }
}

Age will return: 18.01

Dgraph metadata

dgraph version

PASTE THE RESULTS OF dgraph version HERE.

I would appreciate any assistance or suggestions on how to address these challenges.

Specifically, I am looking for ways to sort users by distance and calculate the age gap directly in the Dgraph query.

Thank you in advance for your help.

Best Regards,
Thang Pham

If you are in the early stage of application development, I would recommend you to switch to a different database. DGraph’s development has been inconsistent over the past two years and its future is also very uncertain. There are many other graph databases out there which are fast and well maintained

3 Likes

Thanks for your advice, I’m considering using a different database

Please note that over the past couple of years Dgraph has released two major versions and done a lot of internal stability improvements to the cloud service which are less visible.

The upcoming v24 release due in May allows semantic similarity searches using vectors (the alpha is out now) and will be ideal for recommendation systems.

Hey Damon,

It is hard to recommend an app to someone until there is progress on the actual problems that matter. GraphQL is unusable until someone fixes the @auth security holes (not just a feature request) for example. If you want the people who know Dgraph to recommend the product to newcomers, you guys should start listening to them.

These problems have been promised to be fixed longer than the past couple of years. In fact, from my understanding there already are internal PRs that actually do fix some of them.

Maybe start there…

J

2 Likes