How to create a SPARQL query to search Wikidata item descriptions - sparql

I'd like to be able to find an item based on words in its descriptions.
This is what I'm trying to do, but clearly I don't know what I'm doing. Any help is appreciated.
SELECT ?item ?itemLabel WHERE {
?item schema:description ?desc.
FILTER(CONTAINS(LCASE(?desc), "space telescope"))
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 10

Solved by UninformedUser in the comments!
WHERE { hint:Query hint:optimizer "None".
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:api "Search";
wikibase:endpoint "www.wikidata.org";
mwapi:srsearch "space telescope".
?item wikibase:apiOutputItem mwapi:title . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
limit 10

Related

Wikidata Sparql get item descriptions

I'm following the MWAPI docs (https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual/MWAPI#Find_all_entities_with_labels_%22cheese%22_and_get_their_types) and have the following query:
SELECT * WHERE {
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:endpoint "www.wikidata.org";
wikibase:api "EntitySearch";
mwapi:search "python";
mwapi:language "en".
?item wikibase:apiOutputItem mwapi:item.
?label wikibase:apiOutputItem mwapi:label.
?num wikibase:apiOrdinal true.
}
}
ORDER BY ASC(?num) LIMIT 10
I also want to retrieve the description of the wikidata item but don't know how to get it. I've tried adding the following line ?description wikibase:apiOutputItem mwapi:description but it seems to be an invalid property on the API. How do I retrieve it and where can I find this in the docs?
Query reproducible here: https://query.wikidata.org/#SELECT%20%2a%20WHERE%20%7B%0A%20%20SERVICE%20wikibase%3Amwapi%20%7B%0A%20%20%20%20%20%20bd%3AserviceParam%20wikibase%3Aendpoint%20%22www.wikidata.org%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20wikibase%3Aapi%20%22EntitySearch%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mwapi%3Asearch%20%22python%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20mwapi%3Alanguage%20%22en%22.%0A%20%20%20%20%20%20%3Fitem%20wikibase%3AapiOutputItem%20mwapi%3Aitem.%0A%20%20%20%20%20%20%3Flabel%20wikibase%3AapiOutputItem%20mwapi%3Alabel.%0A%20%20%20%20%20%20%3Fdesc%20wikibase%3AapiOutputItem%20mwapi%3Adescription.%0A%20%20%20%20%20%20%3Fnum%20wikibase%3AapiOrdinal%20true.%0A%20%20%7D%0A%7D%20%0AORDER%20BY%20ASC%28%3Fnum%29%20LIMIT%2010
The wikibase:label service provides both the label and the description of an item for a specified language. Both have to be explicitly queried for in the SELECT part:
SELECT ?item ?itemLabel ?itemDescription ?num WHERE {
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:endpoint "www.wikidata.org";
wikibase:api "EntitySearch";
mwapi:search "python";
mwapi:language "en".
?item wikibase:apiOutputItem mwapi:item.
?num wikibase:apiOrdinal true.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ASC(?num) LIMIT 10

Getting labels/names and invert wikidata query

I'am trying to get the names of the victims, which got a "stolperstein" (stumblestone) in my hometown. But I can't get the names with SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
I think, the query should be
SELECT ?Stolperstein ?StolpersteinLabel ?bild ?Person ?tod WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?Stolperstein wdt:P31 wd:Q26703203;
wdt:P131 wd:Q2107;
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
wdt:P547 ?Person.
But it doesn't work. What am I doing wrong?
My second question is, how can I display the data with missing information. (invert the query / like NOT IN)
SELECT ?Stolperstein ?StolpersteinLabel ?bild ?Person ?tod WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?Stolperstein wdt:P31 wd:Q26703203;
wdt:P131 wd:Q2107;
wdt:P547 ?Person.
?Person wdt:P570 ?tod
gives me only 88 results, but there are 188 "stones", so therefore in 100 datasets is the information date of death (P570) missing.

WIKIDATA/SPARQL: How to get the used language when using SERVICE wikibase:label with [AUTO_LANGUAGE] parameter

About everything in the title : When I use
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } I may get some results with the detected [AUTO_LANGUAGE] and others in 'en'.
Is it possible to get the used language for each of the lines of the result ?
Yes. LANG(?var) does that:
SELECT ?item ?itemLabel (LANG(?itemLabel) as ?langLabel)
WHERE {
?item wdt:P31 wd:Q146.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "de,fr,en".
}
}

Find which direct property applied in a SPARQL query

I have a list of properties I want to apply to a specific entity mathematics: wd:Q395. In this case:
instanceOf: 'wdt:P31'
subclassOf: 'wdt:P279'
The results are:
Mathematics is instance of academic discipline and
Mathematics is subclass of exact science and formal science
Instead of making two different queries I would like to make them all at once:
SELECT ?field ?fieldLabel ?propertyApplied
WHERE {
wd:Q395 wdt:P31 | wdt:P279 ?field.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
BIND("" AS ?propertyApplied)
}
How can I know which property applied to fill the right column? ( for example next to academic discipline I would like that it appears instance of)
I tried this but it looks weird and the results repeat themselves.
SELECT ?instanceOf ?subclassOf ?instanceOfLabel ?subclassOfLabel
WHERE {
OPTIONAL { wd:Q395 wdt:P31 ?instanceOf. }
OPTIONAL { wd:Q395 wdt:P279 ?subclassOf. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Use VALUES or UNION:
SELECT ?field ?fieldLabel ?propertyLabel WHERE {
VALUES (?predicate) {(wdt:P31) (wdt:P279)}
wd:Q395 ?predicate ?field .
?property wikibase:directClaim ?predicate .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!
SELECT ?field ?fieldLabel ?propertyLabel {
{ wd:Q395 wdt:P31 ?field . BIND (wd:P31 AS ?property) }
UNION
{ wd:Q395 wdt:P279 ?field . BIND (wd:P279 AS ?property) }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!

Wikidata: list every physical objects

I'm trying to get the name of the all physical things (tangible concepts) Wikidata knows about (objects, places, countries, etc), or in other words everything non-abstract.
There are examples close to what I need, but with only a depth of one: all the things that are instances of phone.
I found this example that searches with more depth and I modified the start point to entity:
#Children of Genghis Khan
#added before 2016-10
#defaultView:Graph
PREFIX gas: <http://www.bigdata.com/rdf/gas#>
SELECT ?item ?itemLabel ?pic ?linkTo
WHERE
{
SERVICE gas:service {
gas:program gas:gasClass "com.bigdata.rdf.graph.analytics.SSSP" ;
gas:in wd:Q35120 ;
gas:traversalDirection "Forward" ;
gas:out ?item ;
gas:out1 ?depth ;
gas:maxIterations 4 ;
gas:linkType wdt:279 .
}
OPTIONAL { ?item wdt:P40 ?linkTo }
OPTIONAL { ?item wdt:P18 ?pic }
SERVICE wikibase:label {bd:serviceParam wikibase:language "en" }
}
I still get no results.
As mentionned in a comment, your question is too broad and you'll end up with too much answers
You look for the A that are instance of a subclass of B
so the query you point to is the right one
SELECT DISTINCT ?item
WHERE {
?item wdt:P31/wdt:P279* wd:Q35120
}
the problem is the size
wd:Q35120 has a lot of subclasses
You can check that this way
SELECT ?a ?aLabel WHERE { ?a wdt:P279 wd:Q35120.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
SELECT ?a ?aLabel WHERE { ?a wdt:P279/wdt:P279? wd:Q35120.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
SELECT ?a ?aLabel WHERE { ?a wdt:P279/wdt:P279?/wdt:P279? wd:Q35120.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
And so on : you'll see that there are already 40'000+ at forth level which is huge
You could also huge this nice tool to have a more precise view
https://tools.wmflabs.org/bambots/WikidataClasses.php?id=Q35120&lang=en