Skip to main content

Wikidata: Continued

For this section, we'll take a closer look at the challenge query from last time, and spend some time walking through the query triple-by-triple.

SELECT ?name ?artist ?artwork ?artworkLabel
WHERE {
?person fd:person/handle "jdoe";
fd:person/favArtists ?artist.
?artist fd:artist/name ?name.
?artwork wdt:P170 ?creator.
?creator wd:?label ?name.
}

The first three triples should be fairly easy, as they just use Fluree data.

  1. ?person fd:person/handle "jdoe";

    First, we bind ?person to the subject id that matches person/handle is jdoe. There can only be one subject that matches this pattern in our ledger.

  2. ... fd:person/favArtists ?artist.

    Then, we bind all of Jane Doe's favorite artists' subject ids to ?artist. This binds just the artists' subject ids, because person/favArtists is a predicate of type ref.

  3. ?artist fd:artist/name ?name.

    In order to get all artists' names, we need to get their artist/name, and we bind all of those names to ?name.

    The last two triples use Wikidata.

  4. ?artwork wdt:P170 ?creator.

    Both ?artwork and ?creator are completely new variables that haven't been bound to any data yet. In this triple, we are looking for any Wikidata that match the pattern "anything (?artwork) created by (wdt:P170) anyone (?creator).

  5. ?creator wd:?label ?name.

    This final triple connects our Wikidata data with our Fluree. ?creator is already bound to any creator in Wikidata. ?name is bound to all of Jane Doe's favorite artists.

?label has the prefix wd:, just to let Fluree know that this triple should be processed using Wikidata. The prefix could just as easily been put on wd:?creator or wd:?name.

There are many different predicates in Wikidata that correspond to the way things are named (alternate spellings, different labelling schemes, etc). To simplify this, we just say that any ?creator with any predicate where the object is ?name should be returned. In practice, this only returns ?creators whose names/labels are ?name, because almost all other predicates would return a Q-id, instead of a value.

If this is confusing, that's okay! If you're using Wikidata extensively, we recommend diving deeply into their resources. If you want to use Wikidata for one to two queries, you can just use examples queries + modify until you're happy.