Value variables a litte more explained? I have got a misunderstanding

Hi, e.g we have this schema:
image

now we have the query

{
  # 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?

image

Wait I think I got it

a as rating

a gets sum up, so 100 5stars ratings are 500stars

and the avg as math(a/c) which is 500/100 = an average of 5 stars

yea that makes sense

so i think my misunderstanding is, when edges get executed/the edges get traversed. can someone maybe explain that to me?

I thought when dgraph retrieves fields, and then comes to an edge, it automatically fetches that edge instantly.

So, does dgraph/DQL first query all fields, and then fetch the edges? that feels wrong to me…

I feel that it automatically instantly fetches edges. So therefore avg is just overwritten 100 times. is that correct?

but avg getting overwritten also feels quite wrong. maybe it’s a map of values? but that is also false I think

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

1 Like
# 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 :+1:

Okay, don’t take everything for granted. Because I didn’t understand everything you said. Just some parts. :stuck_out_tongue:

1 Like