Search code examples
sparqlwikidata-query-service

Wikidata Query Service traverse property


I am trying to get a list of all hyperonymes of a given Wikidata item. For instance I want to traverse the "subclass of" element for https://www.wikidata.org/wiki/Q4421.

How would the SPARQL command for https://query.wikidata.org look like?


Solution

  • You can use the property path (*) syntax.

    In this case wdt:P279* to mention 0 or more time wdt:P279 which is subclass of equivalent in Wikidata model.

    I added a few variation here to include the depth so you can order them

    SELECT ?entity ?entityLabel (count(?mid) as ?depth) WHERE {
      wd:Q4421 wdt:P279* ?mid.
      ?mid wdt:P279* ?entity
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    } group by ?entity ?entityLabel
    order by ?depth
    

    https://query.wikidata.org/#SELECT%20%3Fentity%20%3FentityLabel%20%28count%28%3Fmid%29%20as%20%3Fdepth%29%20WHERE%20%7B%0A%20%20wd%3AQ4421%20wdt%3AP279%2a%20%3Fmid.%0A%20%20%3Fmid%20wdt%3AP279%2a%20%3Fentity%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%0A%20%20%20%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20.%0A%20%20%20%7D%0A%7D%20group%20by%20%3Fentity%20%3FentityLabel%0Aorder%20by%20%3Fdepth