List predicate order

What I want to do

I want to understand if list predicates (starring: [uid] .) have a defined order, e.g. same order as they were added.

What I did

Tried updating the predicate with different order, but result was always the same.

<uid1> <starring> <uid2> .
<uid1> <starring> <uid3> .
# vs
<uid1> <starring> <uid3> .
<uid1> <starring> <uid2> .

I’m assuming the list predicate order can’t be controlled, but can someone confirm this?

Dgraph metadata

dgraph version

v21.12.0

Yes. You can’t control the order in a list type. But in this case, it is a list of UIDs. UID is go uint64 and in Dgraph’s context they will assume the UID order fist and then you can use Sorting Sorting - Query language

1 Like

I also have an application where the order of UIDs is important. I use facets as a workaround:

<uid1> <starring> <uid2> (sequence=0).
<uid1> <starring> <uid3> (sequence=1).

You can restore the order by parsing out the facet. Let’s say you run this DQL query:

q(func: uid(0x123)) {
       uid starring @facets { uid }
}

You would get json like this:

{"q": [
    {"uid": "0x123",
     "starring": [
          {"uid": "0x125",
           "starring|sequence": "1"},
          {"uid": "0x124",
           "starring|sequence": "0"}
          ]
    }
  ]
}

Now you have to parse out the sequence facet. I know is a bit of a hassle, but it works for me.

Hi,

Can’t you simply sort with facets directly with DQL?

https://dgraph.io/docs/query-language/facets/#sorting-using-facets

can you provide a query?

From the linked doc above

curl -H "Content-Type: application/dql" localhost:8080/query -XPOST -d '{
  me(func: anyofterms(name, "Alice Bob Charlie")) {
    name
    rated @facets(orderdesc: rating) {
      name
    }
  }
}' | python -m json.tool | less
1 Like

Note that the sorting only works if the predicate is a list UID predicate. You cannot sort list strings this way