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!
Related
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
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".
}
}
I was learning the wikidata query language, and wanted to find the US president's name along with grand father, great grand father and so on...
I tried this to get a father, but how to find the father's father and so on...
SELECT ?valLabel ?resLabel
WHERE {
?val wdt:P31 wd:Q5.
?val wdt:P27 wd:Q30.
?val wdt:P106 wd:Q82955.
?val wdt:P22 wd:Q11806.
OPTIONAL { ?val wdt:P22 ?res. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Your query is looking for humans who are US citizens who are politicians and whose father is John Adams.
Instead, you want a query like this (in pseudo-SPARQL):
SELECT ?presidentLabel ?fatherLabel ?gFatherLabel ?ggFatherLabel ...
WHERE {
?president position_held president_of_the_US .
OPTIONAL{?president has_father ?father .}
OPTIONAL{?father has_father ?gFather .}
OPTIONAL{?gFather has_father ?ggFather .} ...
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Notice that the optional is necessary, as there may be no information up to the great-grandfather of a president, in which case no information about the president would be returned at all.
In Wikidata, the following should work:
SELECT
?presidentLabel
?fatherLabel
?gFatherLabel
?ggFatherLabel
WHERE {
?president wdt:P39 wd:Q11696.
OPTIONAL{?president wdt:P22 ?father}
OPTIONAL{?father wdt:P22 ?gFather}
OPTIONAL{?gFather wdt:P22 ?ggFather}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
However the query seems to time out when you look beyond the president's grandfather.
Bonus: If you want the presidents in chronological order, use this query:
SELECT ?ord ?presidentLabel ?fatherLabel ?gFatherLabel
WHERE {
?president wdt:P39 wd:Q11696 ;
p:P39 ?presidency .
?presidency ps:P39 wd:Q11696 ;
pq:P1545 ?ordString .
OPTIONAL{?president wdt:P22 ?father .}
OPTIONAL{?father wdt:P22 ?gFather .}
BIND(xsd:integer(?ordString) AS ?ord)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?ord
Try this query
#Father - GrandFather and so on ...
SELECT ?childLabel ?fatherLabel ?grandFatherLabel ?greatGrandFather1Label ?greatGrandFather2Label
WHERE {
?child wdt:P31 wd:Q5.
?child wdt:P27 wd:Q30.
?child wdt:P106 wd:Q82955.
?child wdt:P22 wd:Q11806.
OPTIONAL { ?child wdt:P22 ?father. }
OPTIONAL { ?father wdt:P22 ?grandFather. }
OPTIONAL { ?grandFather wdt:P22 ?greatGrandFather1. }
OPTIONAL { ?greatGrandFather1 wdt:P22 ?greatGrandFather2. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Here is a query with all fathers (and their fathers, as far back as there is data in Wikidata) of all US presidents:
SELECT ?presidentLabel ?fatherLabel
WHERE
{
?president wdt:P39 wd:Q11696;
wdt:P31 wd:Q5;
wdt:P22+ ?father .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!
I'm trying to perform path matching inside a VALUES clause in sparql in order to match all instances and subclasses of both battles and sieges in wikidata. The following request repeatedly times out.
SELECT DISTINCT ?battle ?battleLabel WHERE {
{
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
VALUES ?type {wd:Q178561 wd:Q188055} ?battle (wdt:P31/wdt:P279*) ?type .
?battle rdfs:label ?queryByTitle.
FILTER(REGEX(?queryByTitle, "saratoga", "i"))
}
}
It seems that VALUES, esp. in conjunction with /, confuses the Blazegraph's query optimizer in that case.
Use UNION instead of VALUES:
SELECT DISTINCT ?battle ?battleLabel WHERE {
{ ?battle wdt:P31/wdt:P279* wd:Q178561 }
UNION
{ ?battle wdt:P31/wdt:P279* wd:Q188055 }
?battle rdfs:label ?queryByTitle.
FILTER(REGEX(?queryByTitle, "saratoga", "i"))
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
Alternatively, disable the optimizer and specify explicit order:
SELECT DISTINCT ?battle ?battleLabel WHERE {
hint:Query hint:optimizer "None" .
VALUES ?type {wd:Q178561 wd:Q188055}
?subtype wdt:P279* ?type .
?battle wdt:P31 ?subtype .
?battle rdfs:label ?queryByTitle.
FILTER(REGEX(?queryByTitle, "saratoga", "i"))
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
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