I'm wondering if it is possible to use Wikidata label service in a federated query. E.g., the following query
# Query from a local SPARQL enpoint
select ?item ?itemLabel
where {
SERVICE <https://query.wikidata.org/sparql> {
?item wdt:P31 wd:Q146.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
}
returns empty ?itemLabels. How can I get also ?itemLabels in my resultset?
You could also use the manual mode, it's just one line more in your case:
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT * {
SERVICE <https://query.wikidata.org/sparql> {
?item wdt:P31 wd:Q146
SERVICE wikibase:label { bd:serviceParam wikibase:language "it". ?item rdfs:label ?label }
}
}
Try on FactForge
Include some of the things that are nominally optional... Here's the full query I just ran through URIBurner.com, and its results.
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wikibase: <http://wikiba.se/ontology#>
SELECT *
WHERE
{
SERVICE <https://query.wikidata.org/sparql>
{
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P31 wd:Q146 .
SERVICE wikibase:label
{ bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
}
}
Related
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT (SAMPLE(?sport) AS ?sport) ?sportLabel (SAMPLE(?uses) AS ?uses) (SAMPLE(?usesLabel) AS ?usesLabel)
WHERE
{
# instance of sport
?sport wdt:P31 wd:Q31629.
FILTER (!isBlank(?uses))
OPTIONAL { ?sport wdt:P2283 ?uses } .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
GROUP BY ?sportLabel
Not able to get the label of uses. ?usesLabel returns blank strings but ?uses returns correct ids
There are (at least) two options.
(Note: I replaced (SAMPLE(?sport) AS ?sport) with ?sport, as you presumably want to list all sport items.)
Using a subquery
You could use a subquery to aggregate everything, and the outer query to display the labels:
SELECT ?sport ?sportLabel ?usesSample ?usesSampleLabel
WHERE {
{
SELECT ?sport (SAMPLE(?uses) AS ?usesSample)
WHERE {
# instance of sport
?sport wdt:P31 wd:Q31629 .
FILTER (!isBlank(?uses)) .
OPTIONAL { ?sport wdt:P2283 ?uses . }
} GROUP BY ?sport
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
Using Wikidata’s label service
You could keep your current query, and add the labels you need to the SERVICE:
SELECT ?sport ?sportLabel (SAMPLE(?usesLabel) AS ?usesSampleLabel)
WHERE
{
# instance of sport
?sport wdt:P31 wd:Q31629 .
FILTER (!isBlank(?uses)) .
OPTIONAL { ?sport wdt:P2283 ?uses . }
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
?sport rdfs:label ?sportLabel .
?uses rdfs:label ?usesLabel .
}
}
GROUP BY ?sport ?sportLabel
Im trying to perform searchs on wikidata, but my query its a little weird. Some searchs are case-sensitive
EG: using "maradona" match perfectly but "cristiano ronaldo" don't. "Cristiano Ronaldo" match OK.
wikidata
SELECT DISTINCT ?item ?itemLabel (SAMPLE(?RIP) AS ?RIP) (SAMPLE(?image) AS ?image) WHERE {
?item wdt:P31 wd:Q5;
?label "cristiano ronaldo"#en.
?article schema:about ?item;
schema:inLanguage "en".
OPTIONAL { ?item wdt:P570 ?RIP. }
OPTIONAL { ?item wdt:P18 ?image. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?item ?itemLabel
I expect to be able to search all with lowercase (or other way to handle it better)
I am trying to run a simple query, looking up an item by its English label. For some reason, some entries are not showing. Most are fine, but some aren't. I can't pinpoint why.
An example: oyster bed. Here is the entry: https://www.wikidata.org/wiki/Q65953972
This is the query:
SELECT distinct ?item ?itemLabel ?itemDescription WHERE {
?item ?label 'oyster bed'#en.
?article schema:about ?item .
?article schema:inLanguage 'en' .
SERVICE wikibase:label { bd:serviceParam wikibase:language 'en'. }
}
and I get nothing.
The same query with oyster yields an expected result.
What am I doing wrong?
Hat tip of #UninformedUser helped.
As a SPARQL newbie, I did not realise the ?article was an implicit JOIN.
SELECT distinct ?item ?itemLabel ?itemDescription WHERE {
?item ?label 'oyster bed'#en.
OPTIONAL { ?article schema:about ?item .
?article schema:inLanguage 'en' . }
SERVICE wikibase:label { bd:serviceParam wikibase:language 'en'. }
}
is to be used if the article is required, but if there is no real need, the query can be simplified to:
SELECT distinct ?item ?itemLabel ?itemDescription WHERE {
?item ?label 'oyster bed'#en.
SERVICE wikibase:label { bd:serviceParam wikibase:language 'en'. }
}
I am trying to get labels in multiple languages from Wikidata's SPARQL endpoint. The following example is given here:
SELECT ?country ?country_EN ?country_DE ?country_FR
WHERE {
?country wdt:P31 wd:Q185441. # member state of the European Union
SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
?country rdfs:label ?country_EN.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "de".
?country rdfs:label ?country_DE.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr".
?country rdfs:label ?country_FR.
}
}
Try it here
However, this returns the following error:
Unknown error: there can be only one "run last" join in any group
Is there a solution to get labels in more than one language?
rdfs:label can be used directly without the wikibase:label service:
SELECT ?country ?country_en ?country_de ?country_fr
WHERE {
?country wdt:P31 wd:Q185441. # member state of the European Union
OPTIONAL {?country rdfs:label ?country_en filter (lang(?country_en) = "en")}.
OPTIONAL {?country rdfs:label ?country_de filter (lang(?country_de) = "de")}.
OPTIONAL {?country rdfs:label ?country_fr filter (lang(?country_fr) = "fr")}.
}
Try it here
The label service optimizer adds a hint:Prior hint:runLast true hint to the label service unless there’s another explicit hint:
LabelServiceUtils.getLabelServiceNodes(op).forEach(service -> {
if (service.getProperty(QueryHints.RUN_LAST) != null ||
service.getProperty(QueryHints.RUN_FIRST) != null) {
return;
}
service.setProperty(QueryHints.RUN_LAST, TRUE);
});
One should just add hint:Prior hint:runLast false to all the label service invocations after the first one.
Your query should be:
SELECT ?country ?country_EN ?country_DE ?country_FR
WHERE {
?country wdt:P463 wd:Q458. # member state of the European Union
SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
?country rdfs:label ?country_EN.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "de".
?country rdfs:label ?country_DE.
} hint:Prior hint:runLast false.
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr".
?country rdfs:label ?country_FR.
} hint:Prior hint:runLast false.
}
Try it!
Obviously, it is possible to fetch labels in multiple languages using regular SPARQL, and this is less verbose. However the label service provides language fallbacks, including the final one to Q-id's.
Source:
https://phabricator.wikimedia.org/T175840
I can get item and its properties by label:
SELECT distinct ?item ?itemLabel ?itemDescription
(SAMPLE(?DR) as ?DR) (SAMPLE(?article)as ?article)
WHERE {
?item wdt:P31 wd:Q5.
?item ?label "Einstein"#en
OPTIONAL{?item wdt:P569 ?DR .}
?article schema:about ?item .
?article schema:inLanguage "en" .
?article schema:isPartOf <https://en.wikipedia.org/>.
OPTIONAL{?item wdt:P570 ?RIP .}
OPTIONAL{?item wdt:P18 ?image .}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?item ?itemLabel ?itemDescription
See on Wikidata Query Services.
How can I do the same using QID instead label?
Using the URI instead of the variable ?item will get the information based on the entity Albert Einstein:
PREFIX schema: <http://schema.org/>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
SELECT DISTINCT ?item ?itemLabel ?itemDescription (SAMPLE(?DR) AS ?DRSample) (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/Q937> )
OPTIONAL
{ ?item wdt:P569 ?DR }
OPTIONAL
{ ?item wdt:P570 ?RIP }
OPTIONAL
{ ?item wdt:P18 ?image }
SERVICE wikibase:label
{ bd:serviceParam
wikibase:language "en"
}
}
GROUP BY ?item ?itemLabel ?itemDescription
You can utilize the already known QID by using BIND:
BIND(wd:Q937 AS ?item).
...
If you already have the QID of the entity you are looking for and simply look for its properties and labels, you're better off using the Wikidata API wbgetentities module
In A. Einstein (Q937) case, that would give the following API call:
https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q937&format=json