Wikidata Query Service: how do I search by item?

1.9k views Asked by At

I am using the Wikidata Query Service (https://query.wikidata.org) to get details about movies and Tv shows.

I know I can query for genre of all items that are instance of film (query below) but I want to look for just specific movies.

SELECT ?item ?itemLabel ?genreLabel
WHERE
{
    ?item wdt:P31 wd:Q11424 .
    OPTIONAL {
        ?item wdt:P136 ?genre
    }
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
} LIMIT 10

I have a list of the Wikidata item numbers (Q###) for movies and Tv shows that I want to get properties about. I need to query these specific films or TV shows. For example, if I know that Star Wars Episode IV: A New Hope is Q17738 in Wikidata, how can I query for specific properties of it?

3

There are 3 answers

0
user205512 On

There are (at least) two ways of specifying multiple items in SPARQL.

FILTER (?item IN (...list...)): SPARQL definition

SELECT ?item ?itemLabel ?genreLabel
WHERE
{
    ?item wdt:P31 wd:Q11424 .

    FILTER (?item IN (wd:Q17738, wd:Q181795))

    OPTIONAL {
        ?item wdt:P136 ?genre
    }
    SERVICE wikibase:label { bd:serviceParam wikibase:language  
                                "[AUTO_LANGUAGE],en" }
} LIMIT 10

VALUES ?item { ...list... }: SPARQL definition

SELECT ?item ?itemLabel ?genreLabel
WHERE
{
    ?item wdt:P31 wd:Q11424 .

    VALUES ?item { wd:Q17738 wd:Q181795 }

    OPTIONAL {
        ?item wdt:P136 ?genre
    }
    SERVICE wikibase:label { bd:serviceParam wikibase:language  
                                "[AUTO_LANGUAGE],en" }
} LIMIT 10

The difference is that IN is simply a function: is argument in this list?

VALUES is a way to pass in data to a query. Although we used the simple form you can provide full, tabular data:

VALUES (?x ?y) {
   (:uri1 1)
   (:uri2 UNDEF)
}

The syntax has a special case for one variable, so I didn't have to write VALUES ?item { (wd:Q17738) (wd:Q181795) }.

0
Alexan On

Yes, you can get properties using QID, see answer to my similar question, using filtering by URI.

Example for your item Q17738:

SELECT DISTINCT  ?item ?itemLabel ?itemDescription (SAMPLE(?article) AS ?articleSample)
WHERE
  { ?article  schema:about       ?item ;
              schema:inLanguage  "en" ;
              schema:isPartOf    <https://en.wikipedia.org/>
    FILTER ( ?item = <http://www.wikidata.org/entity/Q17738> )    
    SERVICE wikibase:label
      { bd:serviceParam
                  wikibase:language  "en"
      }
  }
GROUP BY ?item ?itemLabel ?itemDescription

See this query in WikiData.

See also answer on Open Data site.

0
maxlath On

You could get properties about a given entity from the SPARQL endpoint, but you are probably better off querying the wbgetentities Wikidata API module.

Ex: https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q17738&languages=en|de|fr&format=json

See also: Getting readable results from Wikidata