Search Wikidata SPARQL for movies or tv series by title - sparql

I want to search "Star Wars" across both TV series (http://www.wikidata.org/entity/Q5398426) and films (http://www.wikidata.org/entity/Q11424). The following SPARQL is for searching movies:
# search movies by name
SELECT DISTINCT ?item ?name WHERE {
?item wdt:P31 wd:Q11424.
?item rdfs:label ?queryByTitle.
OPTIONAL { ?item wdt:P1476 ?name. }
#FILTER(REGEX(?queryByTitle, "star wars", "i"))
FILTER(contains(lcase(?queryByTitle), "star wars"))
}
LIMIT 100
Any idea how I can search by TV Series as well?

#AKSW answered the question. Here's the full gist for anyone searching in the future.
SELECT DISTINCT ?item ?name WHERE {
VALUES ?type {wd:Q5398426 wd:Q11424} ?item wdt:P31 ?type .
?item rdfs:label ?queryByTitle.
OPTIONAL { ?item wdt:P1476 ?name. }
FILTER(REGEX(?queryByTitle, "star wars", "i"))
}
LIMIT 100

Related

Wikidata query for items with one Wikipedia page not in English

I want to find Wikidata items, with each referring to exactly one Wikipedia page which is not an en. Wikipedia page.
I came up with this query:
SELECT ?item WHERE {
?article schema:about ?item .
FILTER (SUBSTR(str(?article), 9, 2) != "en") .
{
SELECT ?item (COUNT(DISTINCT ?lang) AS ?count) WHERE {
?item wdt:P1367 ?yp_id . # BBC 'Your paintings' artist identifier
?article schema:about ?item .
FILTER (SUBSTR(str(?article), 11, 15) = ".wikipedia.org/") .
?article schema:inLanguage ?lang .
} GROUP BY ?item
HAVING (?count=1)
ORDER BY DESC (?count)
}
}
It executes. However, I always get a timeout.
Is there a better query to achieve what I am looking for?
Here's some tip:
Since you take only ?count=1, there is no reason to order by ?count.
Since for each article you can have only one ?lang, you can count by ?article without considering a redundant variable.
Instead of working on (sub)strings, just use the schema:isPartOf property for selecting the specific domain that you want to exclude.
Use FILTER NOT EXISTS instead of FILTER (... != ...)
The fourth optimiziation is the most important and it is sufficient per se.
SELECT ?item WHERE {
FILTER NOT EXISTS {
?article schema:about ?item ;
schema:isPartOf <https://en.wikipedia.org/> .
}
{
SELECT ?item (COUNT(DISTINCT ?article) AS ?count) WHERE {
?item wdt:P1367 ?yp_id . # BBC 'Your paintings' artist identifier
?article schema:about ?item .
FILTER (SUBSTR(str(?article), 11, 15) = ".wikipedia.org/") .
}
GROUP BY ?item
HAVING (?count=1)
}
}

SPARQL query that returns Wikidpedia labels from Wikidata itemLabel

I am new to SPARQL,
Is it possible to write a query that returns Wikipedia box information for a corresponding item label from the Wikipedia box for the Arabic Language that appears at the bottom of the Wikidata item page?
see the picture:
Instead of the Wikipedia URL in the following Query, I need to return the Wikipedia Label, in our case (الرامة (جنين))
Try Query on Wikidata Query Service
SELECT DISTINCT ?article ?item ?itemLabel ?itemDescription ?entity_type ?main_category (GROUP_CONCAT(DISTINCT(?altLabel); separator = ", ") AS ?altLabel_list) WHERE {
?item ?label "الرامة"#ar.
?item wdt:P31 ?entity_type .
MINUS { ?item wdt:P31 wd:Q4167410}
OPTIONAL{ ?item wdt:P910 ?main_category}
?article schema:about ?item;
schema:isPartOf <https://ar.wikipedia.org/>;
OPTIONAL { ?item skos:altLabel ?altLabel . FILTER (lang(?altLabel) = "ar") }
SERVICE wikibase:label { bd:serviceParam wikibase:language "ar" .}
}
GROUP BY ?article ?item ?itemLabel ?itemDescription ?entity_type ?main_category
This is the answer by the UninformedUser
> SELECT ?article ?wikipediaLabel WHERE
> { ?article schema:about wd:Q12187640 . ?article schema:isPartOf <https://ar.wikipedia.org/>; schema:name
> ?wikipediaLabel }

How to query wikidata using SPARQL to find a place that matches certain criteria and is geographically near another city

I wrote the following SPARQL query to find the wikidata item with the label "San Leucio" in Italy.
SELECT DISTINCT * WHERE {
?location ?label 'San Leucio'#en .
?location wdt:P17 wd:Q38 .
?location rdfs:label ?locationName .
OPTIONAL {
?article schema:about ?location .
?article schema:isPartOf <https://en.wikivoyage.org/> .
}
?location wdt:P18 ?image .
FILTER(lang(?locationName) = "en")
}
The query returns these 3 results:
wd:Q55179410
wd:Q20009063
wd:Q846499
The result I want is wd:Q846499, which is outside of Naples, Italy. Is there any way I could further filter this query to return the result that is nearest to Naples? I know that I can get the geoCoordinates for each of these with ?location wdt:P625 ?coordinates, but I'm not sure how I could use that to compare to the geo-coordinates of Naples to get what I want.
SELECT DISTINCT * {
VALUES ?naples {wd:Q2634}
?Napfes wdt:P625 ?naples_coordinates.
?location rdfs:label 'San Leucio'#en .
?location wdt:P17 wd:Q38 .
?location wdt:P18 ?image .
?location wdt:P625 ?location_coordinates.
OPTIONAL {
?article schema:about ?location .
?article schema:isPartOf <https://en.wikivoyage.org/> .
}
BIND (geof:distance(?location_coordinates, ?naples_coordinates) AS ?distance)
} ORDER BY ?distance LIMIT 1

Query Wikidata countries - how to return labels in all languages

How do I query Wikidata countries in order to return the label of all countries in all available languages?
I am able to run the query by specifying the languages I want (e.g. I successfully asked for a list of countries with the English and Arabic labels). But can I ask it to return a label column for all languages? If so, how?
Countries in English and Arabic
SELECT ?country ?label_en ?label_ar
WHERE
{
?country wdt:P31 wd:Q6256.
?country rdfs:label ?label_en filter (lang(?label_en) = "en").
?country rdfs:label ?label_ar filter (lang(?label_ar) = "ar").
}
Sparql Query link
As #AKSW said in a comment, you can get the list of all country names in all languages by removing the filter from your query:
SELECT ?country ?label
WHERE
{
?country wdt:P31 wd:Q6256.
?country rdfs:label ?label
}
You might also (perhaps) want to know the identity of each language being returned, and the English word for each country being named:
SELECT ?country ?label (lang(?label) as ?label_lang) ?countryLabel
WHERE
{
?country wdt:P31 wd:Q6256.
?country rdfs:label ?label .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Why variable + Label is not working in SPARQL

In the examples of the SPARQL of Wikidata, we have this one:
SELECT ?h ?date
WHERE
{
?h wdt:P31 wd:Q5 .
?h wdt:P569 ?date .
OPTIONAL {?h wdt:P570 ?d }
FILTER (?date > "1880-01-01T00:00:00Z"^^xsd:dateTime)
FILTER (!bound(?d))
}
LIMIT 1000
I understand that if you put Label after the name of a variable it shows the label. So, I don't understand why this shows no output:
SELECT ?h ?hLabel ?date ...
Thank you in advance!
I am not aware of that specific feature for Label after the variable name.
However, for rdfs:label, you can to inculde the rdfs:label in your query. Add the following line: ?h rdfs:label ?hLabel.:
SELECT ?h ?hLabel ?date WHERE
{
?h wdt:P31 wd:Q5 .
?h wdt:P569 ?date .
?h rdfs:label ?hLabel.
OPTIONAL {?h wdt:P570 ?d }
FILTER (?date > "1880-01-01T00:00:00Z"^^xsd:dateTime)
FILTER (!bound(?d))
}
LIMIT 1000
If you want labels in a specific language, e.g. for English add FILTER (langMatches( lang(?hLabel), "EN" ) )
Here is a stackoverflow intersting answer about labels.