piro
(小菲)
August 10, 2022, 8:04am
1
What I want to do
Hi guys, I am just starting with Dgraph and I am experiencing “beginners’ troubles”
I’m following this blog post Build a Realtime Recommendation Engine: Part 2 - Dgraph Blog which, of course, I aware it is kinda outdated for Dgraph Zion .
The part 1 went more or less smoothly, but now I’m stuck on an error I don’t really understand:
{
"name": "t",
"url": "https://dgraph.docker/query?timeout=20s",
"errors": [
{
"message": ": Vars can be assigned only at root when grouped by Value",
"extensions": {
"code": "ErrorInvalidRequest"
}
}
]
}
What I did
I have successfully set movielens schema and data on Docker with the command:
dgraph live -f out.rdf.gz -s movielens.schema -c 1 --zero=dgraph-zero:5080 --alpha dgraph-alpha:9080
Running this DQL query:
{
var(func: uid(0x2711)) { # 1
rated @groupby(genre) {
gc as count(uid)
}
a as math(1)
seen as rated @facets(r as rating) @facets(ge(rating, 3)) { # 2
~rated @facets(sr as rating) @facets(ge(rating, 3)) { # 3
user_score as math((sr + r)/a) # 4
}
}
}
var(func: uid(user_score), first:30, orderdesc: val(user_score)) { #5
norm as math(1)
rated @filter(not uid(seen)) @facets(ur as rating) { # 6
genre {
q as math(gc) # 6.1
}
x as sum(val(q)) # 6.2
fscore as math((1+(x/100))*ur/norm) # 7
}
}
Recommendation(func: uid(fscore), orderdesc: val(fscore), first: 10) { # 8
val(fscore)
name
}
}
Can you help me to understand what I’m missing?
Thank you in advance
Dgraph metadata
dgraph version
Dgraph version :
Dgraph codename :
Dgraph SHA-256 : a0722f00f4a6bebb5e5cfdcd49bab68ca29e5ae6eeeb7853a3e1289518ac8b3c
Commit SHA-1 :
Commit timestamp :
Branch :
Go version : go1.17.7
jemalloc enabled : false
MichelDiz
(Michel Diz)
August 10, 2022, 2:33pm
2
piro:
genre
Is genre a value or an edge?
piro
(小菲)
August 10, 2022, 2:52pm
3
Edge.
{
film (func: has(genre)) {
uid
name
genre {
uid
name
}
}
}
{
"data": {
"film": [
{
"uid": "0x30d41",
"name": "Toy Story (1995)",
"genre": {
"uid": "0x186a3",
"name": "Animation"
}
},
{
"uid": "0x30d42",
"name": "GoldenEye (1995)",
"genre": {
"uid": "0x186a1",
"name": "Action"
}
},
{
"uid": "0x30d43",
"name": "Four Rooms (1995)",
"genre": {
"uid": "0x186b0",
"name": "Thriller"
}
},
...
MichelDiz
(Michel Diz)
September 25, 2022, 2:06am
4
What version are you using? I wasn’t able to find that error in the code
Just this one
for _, grp := range res.group {
if len(grp.keys) == 0 {
continue
}
if len(grp.keys) > 1 {
return errors.Errorf("Expected one UID for var in groupby but got: %d", len(grp.keys))
}
uidVal := grp.keys[0].key.Value
uid, ok := uidVal.(uint64)
if !ok {
return errors.Errorf("Vars can be assigned only when grouped by UID attribute")
}
// grp.aggregates could be empty if schema conversion failed during aggregation
if len(grp.aggregates) > 0 {
tempMap[uid] = grp.aggregates[len(grp.aggregates)-1].key
}
}
doneVars[chVar] = varValue{
Vals: tempMap,
path: append(path, pathNode),
}
Update.
I have found it
dgraph-io:master
← dgraph-io:minhaj/groupby
opened 04:19AM - 23 Apr 21 UTC
This PR extends support for var inside the @groupby query on a scalar predicate … at root.
for example the given query now doesn't result to error:-
```
var(func: ... ) @groupby(age) {
c as count(uid)
}
```
Now the uid variable `c` contains map of uid to count of uids with the same age.
Suppose for the data :-
```{
"uid": "0x1",
"age": 38
},
{
"uid": "0x17",
"age": 15
},
{
"uid": "0x18",
"age": 15
},
{
"uid": "0x19",
"age": 17
}
```
The following groupby query:-
```
var(func: uid(1, 23, 24, 25)) @groupby(age) {
c as count(uid)
}
me(func: uid(c)) {
uid
val(c)
}
```
returns the result:-
```
"data": {
"me": [
{
"uid": "0x1",
"val(c)": 1
},
{
"uid": "0x17",
"val(c)": 2
},
{
"uid": "0x18",
"val(c)": 2
},
{
"uid": "0x19",
"val(c)": 1
}
]
}
```
The behaviour will be the same if there are multiple predicates and some of them are uid predicates.
However, it is only supported at root currently. Hence the given query will still return error:-
```
var(func: .....) {
friend @groupby(age) {
a as count(uid)
}
}
```
<!--
Your title must be in the following format: topic(Area): Feature
Topic must be one of build|ci|docs|feat|fix|perf|refactor|chore|test
Sample Titles:
feat(Enterprise): Backups can now get credentials from IAM
fix(Query): Skipping floats that cannot be Marshalled in JSON
perf: [Breaking] json encoding is now 35% faster if SIMD is present
chore: all chores/tests will be excluded from the CHANGELOG
Please add a description with these things:
1. A good description explaining the problem and what you changed.
2. If it fixes any GitHub issues, say "Fixes #GitHubIssue".
3. If it corresponds to a Jira issue, say "Fixes DGRAPH-###".
4. If this is a breaking change, please put "[Breaking]" in the title. In the description, please put a note with exactly who these changes are breaking for.
-->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/dgraph-io/dgraph/7746)
So, this behavior was changed. You need to modify that query manually. The tutorial was made way before that change.