How to get result of a type limited by x correctly?

I am studying the type system and as I saw the doc said

I sent a query

{
  q(func: type(Person), first:5) {
 	 name@en
	}
}

and I got the result

{
  "data": {
    "q": [
      {
        "name@en": "Steve Santa Sekiyoshi"
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 23600,
      "processing_ns": 30901100,
      "encoding_ns": 15100,
      "assign_timestamp_ns": 1101000
    },
    "txn": {
      "start_ts": 60309
    }
  }
}

but when i use this

{
  q(func: has(name@en), first:5) @filter(type(Person))  {
 	 name@en
	}
}

The result is

{
  "data": {
    "q": [
      {
        "name@en": "Steve Santa Sekiyoshi"
      },
      {
        "name@en": "Kirstie Bingham"
      },
      {
        "name@en": "Domenica Cameron-Scorsese"
      },
      {
        "name@en": "William Garcia"
      },
      {
        "name@en": "Coleman Horn"
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 25400,
      "processing_ns": 1506232300,
      "encoding_ns": 27800,
      "assign_timestamp_ns": 1056200
    },
    "txn": {
      "start_ts": 60999
    }
  }
}

This really confused me. The “first: 5” in first query seems skipping some node that is not the type of “Person”. So it skipped first 4 node that is not type “Person” and return the 5th one that is a type “Person”.

All of your entities of Persons has the "dgraph.type": "Person"
OR in RDF <0x1> <dgraph.type> "Person" . in the dataset?

{
  q(func: has(name@en), first:5) @filter(type(Person))  {
 	 	name@en
    dgraph.type
	}
}
{
  "data": {
    "q": [
      {
        "name@en": "Steve Santa Sekiyoshi",
        "dgraph.type": [
          "Person"
        ]
      },
      {
        "name@en": "Kirstie Bingham",
        "dgraph.type": [
          "Person"
        ]
      },
      {
        "name@en": "Domenica Cameron-Scorsese",
        "dgraph.type": [
          "Person"
        ]
      },
      {
        "name@en": "William Garcia",
        "dgraph.type": [
          "Person"
        ]
      },
      {
        "name@en": "Coleman Horn",
        "dgraph.type": [
          "Person"
        ]
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 31500,
      "processing_ns": 1392594800,
      "encoding_ns": 25200,
      "assign_timestamp_ns": 1085900
    },
    "txn": {
      "start_ts": 79430
    }
  }
}

Yeah, this is odd. @martinmr potential bug with Type + pagination.

Looks like an issue indeed. I’ll look into it.

Actually, I just went through the queries again and I don’t think there’s an issue. Let me explain further.

{
  q(func: type(Person), first:5) {
 	 name@en
	}
}

This query is getting the first five nodes with a dgraph.type value. These nodes are not guaranteed to have a name (or more specifically a name with a @en tag). If they don’t they won’t show up in the results. You can make them show up by changing the query to.

{
  q(func: type(Person), first:5) {
    uid
    name@en
  }
}

The other query goes through all the nodes that have a name@en value. Since we tagged all the nodes with a type in this dataset, the five results show up.

Hope this helps clear up why the results of the two queries are different.

3 Likes