{
# M1 -- Star Wars by its unique ID
var(func: uid(0x30d72)) {
g as genre
}
# Calculate the average rating for every movie
var(func: has(rated)) {
allmovies as rated @facets(a as rating) {
c as count(~rated)
avg as math(a / c)
}
}
# Give every movie a score
var(func: uid(allmovies)) {
x as count(genre @filter(uid(g)))
score as math(avg + x)
}
# Return the top 10 movies
fin(func: uid(score), orderdesc: val(score), first: 10) {
name
val(score)
}
}
can someone explain to me,
what exactly this block is doing:
# Calculate the average rating for every movie
var(func: has(rated)) {
allmovies as rated @facets(a as rating) {
c as count(~rated)
avg as math(a / c)
}
}
I of course know it calcs the average rating for every movie, but I don’t understand that block exactly happens inside there
my assumption:
with has(rated) we get all users that ever rated something.
then we save all movie uid within allmovies for later. Question: duplicates are just overwritten, right? since some users of course rate the same movies.
Then we traverse the edge & get the facet and save the rating in the variable a
then we count how often the movie was rated, and save it to the variable c. is that correct?
then we calc the rating of that user, e.g he rated 4 of the 5 stars and the movie has 200 ratings; then we calc 4 / 200 which is 0.002 and store that into avg .
So, where exactly is the average rating for every movie calced now?
I know it has something to do with the value variables, since they get sum up in nested queries. But i dunno how that happens now. Maybe avg is a sum of 0.02 + 0.02 + 0.015 + 0.01 and so on; which does not make any sense. So I have a knowledge gap in my understanding of value variables. can someone explain it to me?
Isn’t possible to have duplicates when you have UID base. If in that block there are for some reason 10 nodes with the same UID, it will be ignored and treated as a single node/entity.
The logic is isolated by hops. e.g. If 10 people rated “Toy Story 1”, it won’t be ignored and treated as one. It will be sumed and give it 10. Cuz there are levels/hops between the root and the nested. In general, only the root objects are kind of “merged”.
I don’t get the question.
I don’t understand what you mean. Edges are “fields”. An edge is part of a node, which is a compound of a “key and a value” along with UID. There are value edges and relational edges.
Dgraph will search only for the Root params of the Root query. The
# Calculate the average rating for every movie
var(func: has(rated)) {
allmovies as rated @facets(a as rating) { # line UL
c as count(~rated)
avg as math(a / c) # line XY
}
}
thanks a lot now I understand!!!
so basically all ratings in line UL are touched by that value variable and therefore saved, and since we go a level down to line XY all these values in the variable a get accumulated (=sum up), so basically rating(which is the variable a in line UL) 4 +1 + 5 + 3 + 2 +4 +1 +5 +3 is calculated! This is how value variables work, very nice!
This is basic dgraph behaviour that should get explained more/better in the docs, or maybe shown with some examples, so that beginners like me won’t have problems understanding value variables (becaue of lack of basic knowledge). Anyway, I created a gotcha in the discord channel explaining that basic behaviour knowledge