Why are date filtered out from results when using sorting?

Hi, I’m wondering why are data filtered out when sorting is used?

Here is example:
Schema:

city: string .
name: string .
state: string @index(exact) .
type: string @index(exact) .

mutation:

{
  set {
    _:book1 <name> "Book 1" .
    _:book1 <city> "NY" .
    _:book1 <state> "USA" .
    _:book1 <type> "paper" .
    _:book2 <name> "Book 2" .
    _:book2 <city> "Bratislava" .
    _:book2 <state> "Slovakia" .
    _:book2 <type> "paper" .
    _:book3 <name> "Book 3" .
    _:book3 <city> "Chicago" .
    _:book3 <type> "paper" .
  }
}

First query returns all tree books:

{
  paperBooks(func: eq(type,"paper")) {
    expand(_all_)
  }
}

result:

{
  "data": {
    "paperBooks": [
      {
        "type": "paper",
        "state": "USA",
        "name": "Book 1",
        "city": "NY"
      },
      {
        "type": "paper",
        "state": "Slovakia",
        "name": "Book 2",
        "city": "Bratislava"
      },
      {
        "type": "paper",
        "name": "Book 3",
        "city": "Chicago"
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 10437,
      "processing_ns": 4036215,
      "encoding_ns": 1085967
    },
    "txn": {
      "start_ts": 44
    }
  }
}

Same query with sorting:

{
  paperBooks(func: eq(type,"paper"),orderasc: state) {
    expand(_all_)
  }
}

Result (book 3 is missing):

{
  "data": {
    "paperBooks": [
      {
        "state": "Slovakia",
        "name": "Book 2",
        "city": "Bratislava",
        "type": "paper",
        "uid": "0xb"
      },
      {
        "state": "USA",
        "name": "Book 1",
        "city": "NY",
        "type": "paper",
        "uid": "0xa"
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 7123,
      "processing_ns": 6755452,
      "encoding_ns": 476294
    },
    "txn": {
      "start_ts": 45
    }
  }
}

Why? Why are not nodes without sorting predicate added to start or end of result set?

Selmeci check this issue
https://github.com/dgraph-io/dgraph/issues/2686

You also can do:

{
  paperBooks(func: eq(type,"paper"), orderasc:name, orderasc:state) {
    expand(_all_)
  }
}

Thanks for hint. I will add something like missing = "" for every node and than it works.

1 Like

I have one problem with this solution. I cannot use pagination :frowning:

Can’t see why not. Can you explain?

Cheers.

It looks like offset is working on first predicate which is same for every node (missing = "").

I tried it on different data set, and result was weird.

Query:

{

                     

                     var(func: eq(element.type,["Boiler_master"])) @cascade {
          ELEMENT as ~element.type.in {
            owner @filter(eq(organization.id,"quantum-as"))
          }
       }

                     hits(func: uid(ELEMENT)) {
                       count(uid)
                     }


                     results(func: uid(ELEMENT),orderasc:sort.by.missing.predicate, orderasc: sorting.by.9e01d147337dff100e7c61a24d385fa4, first: 100, offset: 0) {
                       {
          uid
          elementHash: element.hash
          sorting.by.9e01d147337dff100e7c61a24d385fa4   
          sort.by.missing.predicate
                     }

                   }
          }

and same query with offset 100.

Results are here.

Sorry, It’s not obvious to me. You have more than 200 nodes. Using an offset of 100. What’s weird?

update
What means “sort.by.missing.predicate”? Why is empty? if has no value. There’s no sorting. There is no logical sorting by empty value. Needs to have something meaningful or it will obey some other rule not obvious. You’re using two sorting rules. This may interfere.

Note Without a sort order specified, the results are sorted by uid , which is assigned randomly. So the ordering, while deterministic, might not be what you expected.