简体   繁体   中英

result set of SPARQL query against WIKIDATA ignores offset

I expect to retrieve 10 citizens of Canada, whereas the result set ought to start at the lowest wikidata ID Q... it matches (ie the search should "start" at https://www.wikidata.org/wiki/Q1 ) :

    SELECT DISTINCT ?item ?itemLabel 
    WHERE { 
    ?item wdt:P31 wd:Q5 . 
    ?item wdt:P27 wd:Q16 . 
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } 
    } 
    LIMIT 10 OFFSET 0

But the curent result seems to provide arbitrary findings, eg https://www.wikidata.org/wiki/Q116544 (= ice hockey player Danny Gare)

I have not manually checked any entries for canadian citizens with lower WIKIDATA Q id's than Q116544, but I assume that there are some / many.

What do I have to add to get the expected results?

Not sure why you want this because ordering is expensive, but here we go:

First try

Simply using ORDER BY on the ?item :

SELECT DISTINCT ?item ?itemLabel WHERE { 
  ?item wdt:P31 wd:Q5 . 
  ?item wdt:P27 wd:Q16 . 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } 
} 
ORDER BY ASC(?item)
LIMIT 10 OFFSET 0

The drawback, sorting is done lexicographically...

Workaround

Extract the number from the entity URI with strafter function:

  1. extract value after http://www.wikidata.org/entity/Q with

    strafter(str(?item), "http://www.wikidata.org/entity/Q")

  2. convert to integer value by using XPath constructor function xsd:integer()
  3. BIND to variable

Final Query:

SELECT DISTINCT ?item ?itemLabel WHERE { 
   ?item wdt:P31 wd:Q5 . 
   ?item wdt:P27 wd:Q16 . 
   SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } 
   BIND(xsd:integer(strafter(str(?item), "http://www.wikidata.org/entity/Q")) as ?number)
} 
ORDER BY ASC(?number)
LIMIT 10 OFFSET 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM