Ordering via val variable doesn't seem to work

Schema:

    name: string @index(hash).
    nickname: string @index(hash).
    age: string .
    class: string @index(exact, trigram) .
    type TypeName {
        name: string
        class: string
        nickname: string
        age: int
    }

Data:

        <1> <name> "Anurag" .
        <1> <class> "first" .
        <1> <nickname> "Anu" .
        <1> <age> "10" .
        <1> <dgraph.type> "TypeName" .

        <2> <name> "Brad" .
        <2> <class> "second" .
        <2> <nickname> "BD" .
        <2> <age> "20" .
        <2> <dgraph.type> "TypeName" .

Query:

    q1(func: has(name)) {
        v as name
        a as age
    }
    q2(func: has(name), orderdesc: val(v)) { //Works if I pass orderdesc: name
        name
        age
        val(v)
        val(a)
    }

Output:

{
  "q1": [
    {
      "age": "10", 
      "name": "Anurag"
    }, 
    {
      "age": "20", 
      "name": "Brad"
    }
  ], 
  "q2": []
}

Is there something I am missing, I don’t know why q2 doesn’t seem to work?

I feel this is a bug and not a feature we don’t support because I did see my matrix getting correctly ordered uptil here

You have to do it via multiple blocks
e.g

{    
  G as q1(func: has(name)) {
        v as name
        a as age
    }
    q2(func: uid(G), orderdesc: val(v)) {
        name
        age
        val(v)
        val(a)
    }
}

Without the G reference (that works as a “map”) it won’t apply anything in the query block. The variables are based on levels and a kind of uid mapping.

This avoids things like

{    
   q1(func: has(name)) {
        v as name
        a as age
    }
    q2(func: has(title), orderdesc: val(v)) { 
        name
        age
        val(v)
        val(a)
    }
}

This query above would break anyway. Cuz it doesn’t has references.

It would work if I modify my query to be:

    f as q1(func: has(name)) {
        v as name
        a as age
    }
    q2(func: uid(f), orderdesc: val(v)) { //Works if I pass orderdesc: name
        name
        age
        val(v)
        val(a)
    }

Weird…

You mean the above query would break as well because it doesn’t have a map between two queries. Also, we do not want to order results of one query via variables defined in an entirely different query.

Is that correct understanding?

Yes, and potentially, the second block (using has) would bring more other new nodes. That would break the query. The right way is by “Mapping” it.

yep

2 Likes