ozz
ozz

Reputation: 183

Wikidata - get the full subhierachy of one class as well as all nodes

I need the complete hierarchy that is under Food Q2095. All levels of subclasses should also be included. Additionally all entities that are attached to a (sub)class.

How can I query this?

SELECT ?node WHERE {?node ?pred wd:Q2095}

Direct query at wikidata

Thank you.

Upvotes: 0

Views: 871

Answers (1)

Valerio Cocchi
Valerio Cocchi

Reputation: 1966

Try this:

SELECT ?subclass ?entity ?predicate 
WHERE{
   ?subclass wdt:P279* wd:Q2095
   {?entity ?predicate ?subclass }
  UNION 
  {?subclass ?predicate ?entity }
       }

The first part of the body of the query makes sure your subclass eventually leads to food.

The second looks for entities that are attached to the subclass, i.e. are either the object or the subject of a triple containing the said subclass.

Depending on whether you want to restrict the scope of the predicate. (i.e. avoid rdfs:label and others), you can use something like this too:

?predicate a owl:ObjectProperty .

under the first line of the body of the query.

@UninformedUser's comment shows you how to get the level of the subclass.

Upvotes: 1

Related Questions