Order Results

Hi,

I have a data model that goes like this:

type <Repository> {
	Repository.name
	Repository.active_memberships
}
type <Membership> {
	Membership.role
	Membership.created_at
	Membership.of_user
}
type <User> {
	User.username
}

I want to create a query that returns the active memberships sorted by the role and then by the user name so if I have the following data:

  1. Yue Admin
  2. Spinelsun Admin
  3. Li Viewer
  4. Sakura Editor

The result will be:

  1. Spinelsun Admin
  2. Yue Admin
  3. Sakura Editor
  4. Li Viewer

How can I make that happen?
@MichelDiz, I will be glad for your help

1 Like

Can you provide a sample of your dataset?
BTW, the only way to try some type of sorting by role, is using numbers. Dgraph just sort by numbers, characters, UID and so on. Not by some arbitrary rule.

Giving this query:

query get_repository_members($repoId:string) {
    get_repository_members(func: uid($repoId)) {
        members: Repository.active_memberships {
            membership_id: uid
            role: Membership.role
            Membership.of_user {
                user_id: uid
                username: User.username
            }
        }
    }
}

The result is:

"get_repository_members": [
      {
        "members": [
          {
            "membership_id": "0x186a2",
            "role": "Admin",
            "Membership.of_user": {
              "user_id": "0x186ad",
              "username": "michalush"
            }
          },
          {
            "membership_id": "0x186a3",
            "role": "Admin",
            "Membership.of_user": {
              "user_id": "0x186a4",
              "username": "elico"
            }
          },
          {
            "membership_id": "0x186a5",
            "role": "Viewer",
            "Membership.of_user": {
              "user_id": "0x186a1",
              "username": "jrich"
            }
          }
        ]
      }
    ]

elico should come before michalush, after implementing the sort I need - they are both admins so no sorting here but elico starts with e and michalush with m so elico should be presented first.
Role is a string field so dgraph should be able to sort it alphabetically.

Did you try using orderasc from Dgraph Sorting . Although, it will only allow sorting using predicates at the same level. It should suffice if username is a field inside Membership instead of being a field inside User.

I know that if I will put the username inside membership it will be easy but I can’t do that because I need the connection to the user node.
I would like to be able to order the data without changing the data models.
I thought about something: Maybe you can add to the @normalize directive the orderasc/orederdsc func so it will be possible to popup sub predicates to the root level and then sort the data?

@MichelDiz @rajas what is your opinion?

In theory it would be something like this, but it isn’t supported Multiple sorting via value variable.

{
  Y as var(func:eq(name,"Some Repository C")) {
     active_memberships  (orderasc: roleLevel){
      T as roleLevel
      of_user {
        GetName as name
      }
      GTNM as min(val(GetName))
    }
  }
  q2(func: uid(Y)) {
    name
    members: active_memberships (orderasc: val(T), orderasc: val(GTNM)){
      role
      roleLevel
      of_user {
        uid
        name
      }
    }
  }
}

Result

{
  "name": "t",
  "url": "http://localhost:8080/query?timeout=20s",
  "errors": [
    {
      "message": "line 17 column 60: Multiple sorting only allowed by predicates. Got: Stars",
      "extensions": {
        "code": "ErrorInvalidRequest"
      }
    }
  ]
}

My dataset

{
   "set": [
      {
         "name": "Some Repository C",
         "dgraph.type": "Repository",
         "active_memberships": [
            {
               "role": "Admin",
               "roleLevel": "1",
               "dgraph.type": "Membership",
               "of_user": {
                  "name": "A",
                  "dgraph.type": "User"
               }
            },
            {
               "role": "Admin",
               "roleLevel": "1",
               "dgraph.type": "Membership",
               "of_user": {
                  "name": "B",
                  "dgraph.type": "User"
               }
            },
            {
               "role": "Admin",
               "roleLevel": "1",
               "dgraph.type": "Membership",
               "of_user": {
                  "name": "C",
                  "dgraph.type": "User"
               }
            },
            {
               "role": "Admin",
               "roleLevel": "1",
               "dgraph.type": "Membership",
               "of_user": {
                  "name": "M",
                  "dgraph.type": "User"
               }
            },
            {
               "role": "Viewer",
               "roleLevel": "3",
               "dgraph.type": "Membership",
               "of_user": {
                  "name": "A",
                  "dgraph.type": "User"
               }
            },{
               "role": "Viewer",
               "roleLevel": "3",
               "dgraph.type": "Membership",
               "of_user": {
                  "name": "B",
                  "dgraph.type": "User"
               }
            },{
               "role": "Viewer",
               "roleLevel": "3",
               "dgraph.type": "Membership",
               "of_user": {
                  "name": "C",
                  "dgraph.type": "User"
               }
            },{
               "role": "Viewer",
               "roleLevel": "3",
               "dgraph.type": "Membership",
               "of_user": {
                  "name": "J",
                  "dgraph.type": "User"
               }
            },
            {
               "role": "Editor",
               "roleLevel": "2",
               "dgraph.type": "Membership",
               "of_user": {
                  "name": "A",
                  "dgraph.type": "User"
               }
            },
            {
               "role": "Editor",
               "roleLevel": "2",
               "dgraph.type": "Membership",
               "of_user": {
                  "name": "B",
                  "dgraph.type": "User"
               }
            },
            {
               "role": "Editor",
               "roleLevel": "2",
               "dgraph.type": "Membership",
               "of_user": {
                  "name": "C",
                  "dgraph.type": "User"
               }
            },
            {
               "role": "Editor",
               "roleLevel": "2",
               "dgraph.type": "Membership",
               "of_user": {
                  "name": "Z",
                  "dgraph.type": "User"
               }
            }
         ]
      }
   ]
}

BTW, if you need help and want fast answers. Please, next time try to make the same sample I did above based on your request. And examples of desire results. Cuz it is hard to get in the loop of your request. We have to go through several questions here and there and also cover other things. To be short - Help us to help you :stuck_out_tongue:

Happy holidays.

Cheers.