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