Wikidata query timeout - sparql

I wanted to add the instance of property to this example query to get only items which are an instance of human.
This is the example query:
SELECT ?entityLabel (YEAR(?date) as ?year)
WHERE
{
BIND(MONTH(NOW()) AS ?nowMonth)
BIND(DAY(NOW()) AS ?nowDay)
?entity wdt:P569 ?date .
FILTER (MONTH(?date) = ?nowMonth && DAY(?date) = ?nowDay)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
LIMIT 10
And this my adjusted version with the instance of property:
SELECT ?entityLabel (YEAR(?date) as ?year)
WHERE
{
?entity wdt:P31 wd:Q5 .
BIND(MONTH(NOW()) AS ?nowMonth)
BIND(DAY(NOW()) AS ?nowDay)
?entity wdt:P569 ?date .
FILTER (MONTH(?date) = ?nowMonth && DAY(?date) = ?nowDay)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
LIMIT 10
But also I added just the one line I now get a query timeout.
Does anyone know how I cloud improve my query so that I don't get a timeout.

This will time out because the query service will attempt to start with all wdt:P31 wd:Q5 before limiting them.
You can see more details in:
https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/query_optimization

Related

Wikidata: Filter post codes only current valid

I would like to filter post codes to show only the current active for today.
Problem is there are are cities with old post codes (Example).
My current query shows the old post codes:
SELECT ?city ?cityLabel ?postcode ?federal_stateLabel ?federal_state_nr WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de". }
?city (wdt:P31/(wdt:P279*)) wd:Q7930989;
wdt:P17 wd:Q183;
wdt:P281 ?postcode;
wdt:P131 ?federal_state.
?federal_state wdt:P439 ?federal_state_nr.
}
ORDER BY (?postcode)
LIMIT 10
(query.wikidata.org)
I would have to use start time P580 and end time P582 but I don't see how.
You can use this for filtering out the claims which have an end time:
?city p:P281 ?postCodeStmt .
?postCodeStmt ps:P281 ?postcode .
FILTER NOT EXISTS { ?postcode pq:P582 ?endTime . }
The whole query becomes:
SELECT ?city ?cityLabel ?postcode ?federal_stateLabel ?federal_state_nr WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de". }
?city (wdt:P31/(wdt:P279*)) wd:Q7930989;
wdt:P17 wd:Q183;
p:P281 ?postCodeStmt;
wdt:P131 ?federal_state.
?federal_state wdt:P439 ?federal_state_nr.
?postCodeStmt ps:P281 ?postcode.
FILTER NOT EXISTS { ?postCodeStmt pq:P582 ?endTime . } # Filtering out old postal codes
}
ORDER BY (?postcode)
LIMIT 100
See also Wikidata:SPARQL tutorial§Qualifiers.

How do I query Wikidata for items modified in the last day?

I am trying to retrieve all wikidata items within a latitude/longitude bounding box that have been edited in the last 24 hours
I get through the bounding box part ok, but I can't figure the "modified since" bit.
This is the closest example I could find, from which I tried to make this work:
SELECT ?place WHERE {
SERVICE wikibase:box {
?place wdt:P625 ?location .
# looks like cornerwest must be south of cornereast
# otherwise you go around the globe
# this is lng lat
bd:serviceParam wikibase:cornerWest "Point(SW_LNG SW_LAT)"^^geo:wktLiteral .
bd:serviceParam wikibase:cornerEast "Point(NE_LNG NE_LAT)"^^geo:wktLiteral .
}
?place wdt:P31 ?placeCategory .
?place wdt:P625 ?placeCoords .
optional{ ?place wdt:P18 ?placePicture . }
BIND (now() - ?modified as ?date_range)
FILTER (?date_range > 2)
}
without results.
Use schema:dateModified. By the way, Blazegraph supports date and time arithmetic:
SELECT ?place ?placeLabel ?location WHERE {
BIND ((now() - "P7D"^^xsd:duration) AS ?date_)
SERVICE wikibase:box {
?place wdt:P625 ?location .
bd:serviceParam wikibase:cornerSouthWest "Point(55.000 55.000)"^^geo:wktLiteral.
bd:serviceParam wikibase:cornerNorthEast "Point(65.000 65.000)"^^geo:wktLiteral.
}
?place schema:dateModified ?date .
# hint:Prior hint:rangeSafe true .
FILTER (?date >= ?date_)
SERVICE wikibase:label { bd:serviceParam wikibase:language "ru". }
}
https://w.wiki/3E9Z
There is also the wikibase:timestamp predicate which is rather auxiliary.

Filtering URL value

What I am trying to do is to select a certain entity for the certain condition. If the condition is url, then how I can make a proper query to get the only result that matches the v1 = "https://www.getmagicbullet.com/" .
The commented sentences are what I tried different ways, but only to fail. How could I adjust the query to get the right answer?
Thank you for your help.
SELECT DISTINCT ?iLabel ?p ?v1
WHERE {
?i wdt:P31 wd:Q212920.
?i wdt:P856 ?v1.
# FILTER (?v1Label = "https://www.getmagicbullet.com/")
# { ?v1 rdfs:label "https://www.getmagicbullet.com/"#en }
# UNION { ?v1 skos:altLabel "https://www.getmagicbullet.com/"#en }
SERVICE wikibase:label { bd:serviceParam wikibase:language "ko,en,[AUTO_LANGUAGE]". }
}
LIMIT 1000

Querying wikidata for "property constraint"

TL;DR
How to query (sparql) about properties of a property?
Or..
So as part of my project I need to find the properties in wikidata that have any time constraint, to be specific both "start time" and "end time".
I tried this query:
SELECT DISTINCT ?prop WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?person wdt:P31 wd:Q5.
?person ?prop ?statement.
?statement pq:P580 ?starttime.
?statement pq:P582 ?endtime.
}
LIMIT 200
**yeah the properties should be related to humans
Anyway, I do get some good results like:
http://www.wikidata.org/prop/P26
http://www.wikidata.org/prop/P39
But I also get some other properties that definitely wrong.
so, basically what i'm trying to do is to get a list of properties that has the property constraint (P2302) of- allowed qualifiers constraint (Q21510851) with Start time (P580) and End Time (P582)
is that even possible:
I tried some queries like:
SELECT DISTINCT ?property ?propertyLabel ?propertyDescription ?subpTypeOf ?subpTypeOfLabel
WHERE
{
?property rdf:type wikibase:Property .
?property wdt:P2302 ?subpTypeOf.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
but does not get the results I wanted.
is it even possible to query this kind of stuff?
Thanks
Qualifiers are used on property pages too. Your second query should be:
SELECT DISTINCT ?prop ?propLabel {
?prop p:P2302 [ ps:P2302 wd:Q21510851 ; pq:P2306 wd:P580, wd:P582 ] ;
p:P2302 [ ps:P2302 wd:Q21503250 ; pq:P2308 wd:Q5 ; pq:P2309 wd:Q21503252 ] .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
} ORDER BY ASC(xsd:integer(strafter(str(?prop), concat(str(wd:), "P"))))
Try it!
Your first query is correct, but note that this is an 'as-is' query. For example, wd:P410 does not have respective constraints, but look at wd:Q83855.

Recover the "original" order

I am trying to recover the cast list for movies from wikidata.
My SPARQL query for Dr. No is as follows:
SELECT ?actor ?actorLabel WHERE {
?movie wdt:P161 ?actor .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
FILTER(?movie = wd:Q102754)
}
LIMIT 1000
I can try it out at query.wikidata.org but the results are not in the order that I want. It gives 'Sean Connery', 'Zena Marshall', 'Ursula Andress'.
The database has the data in the required order as you can see from https://www.wikidata.org/wiki/Q102754 includes the cast list in order (Sean Connery, Ursula Andress, Joseph Wiseman). Generally the cast list is given in billing order and it is that that I want to recover.
SPARQL provides ordering of results by using ORDER BY, see here
The ordering in your example is based on the number of references of a statement. Here is a non-optimized version that does what you want:
SELECT ?actor ?actorLabel WHERE {
?movie p:P161 ?statement .
?statement ps:P161 ?actor .
OPTIONAL {?statement prov:wasDerivedFrom ?ref . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
FILTER(?movie = wd:Q102754)
}
group by ?movie ?actor ?actorLabel
ORDER BY DESC(count(?ref)) ASC(?actorLabel)
LIMIT 1000