What I want to do
I want to sort nodes by attributes of other nodes that my node type is related to (example below). Bc this must be done by my application for many variations of sorting, it is important to find a way to represent sorted and unsorted query that doesn’t depend on me completely rewriting my query.
I’ll use a toy example to avoid explaining unnecessary details regarding the complexity of my domain.
Consider the model:
Person {
name: string
dad: Person
mom: Person
friends: []Person
}
It’s “easy” to sort by scalars/direct attributes, for example, what if I want to grab all Person ordered by name alphabetically?
persons(func: type(Person), orderasc: name) {
name
}
Then it comes the jump… what if I want to sort Person by dad (or mom) names? Coming from other data store paradigms I was expecting to have some sort of option similar to this. Which doesn’t feel so far-fetched to me considering the 1-1 relationship.
persons(func: type(Person), orderasc: dad.name) {
name
dad {
name // here just to for illustrative purposes
}
}
And ideally, expand on this notion, I would also able to sort even by granddad names… with for example “dad.dad.name”.
And the sorting can grow more complex, one of the cherries on top of that would be… what if I’d like to sort by “friends.name”? Which brings additional complexity given friends are 1-N. What if by “dad.friends.name” or “friends.dad.name”? At this point, I’m just pumping these what-ifs to make further illustrate the situation and complexity.
If from that, we think… ok… we can do all this, then can we order by more than one sorter? E.g. “orderasc: dad.age, orderasc: dad.name” (I threw the age there just to make more sense). Mix and match problems above at will.
What I did
I read through the docs, and the forum. I found a number of references that suggest that this is not possible. Unfortunately, I’m a new user so I can only add 2 links (out of 7) to include more context regarding I’ve read. I chose these 2 as the most important, yet… there’s even a topic opened today asking about sorting by list predicates.
DGraph Forum links indicating issues with DGraph sorting solution at the moment:
- How is sorting done under the hood in DQL? (this one asks a very similar question)
DGraph Forum links with attempts to sort by nested (quite custom, shifts the query around):
What I’d like with this post
First, everything leads me to believe that, besides the simplest of the sorts, none of these are currently possible, and I’d love to have an official confirmation.
Second, I’m fishing for some comments from the DGraph team in regards to the further development of the sorting feature. Is this a recognized weakness? Is there any plans/roadmap to improve sort? Is something even doable in a graph paradigm? And if so, what challenges are there?
Third, does anyone have any idea how to overcome such problems? Bc, right now, my team and I are discussing the feasibility of fetching all the filtered resultset/documents and sort outside of the store, in the application, which is certainly less efficient as lacks indeces, don’t allow for pagination, and, to me, feels virtually comparable to pushing the data out of dgraph into another store (SQL/NoSQL) and read it sorted from there. Disclaimer: the only reason we are considering this is bc we are assuming the result set will be in the order of 100-1000 nodes pushed to a FE app and ordered there, but that’s just the “desperate” solution.
Thanks for the attention!
Dgraph metadata
dgraph version
Dgraph version : v21.03.0
Dgraph codename : rocket
Dgraph SHA-256 : b4e4c77011e2938e9da197395dbce91d0c6ebb83d383b190f5b70201836a773f
Commit SHA-1 : a77bbe8ae
Commit timestamp : 2021-04-07 21:36:38 +0530
Branch : HEAD
Go version : go1.16.2
jemalloc enabled : true