Apparently simple Wikidata SPARQL filter query is slow - sparql

Absolute Wikidata and SPARQL beginner here. I am trying to find out the Q code of a particular female name, say Jennifer. I can get it with a query like this:
SELECT ?name WHERE {
?name wdt:P31 wd:Q11879590.
?name rdfs:label ?label.
FILTER((STR(?label)) = "Jennifer")
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 1
That is, I look up entities that are instance of "female given name" and then filter to those with "Jennifer" in the label. It works, but it takes 5 s or more.
If I omit the LIMIT 1 I get many instances of the same results, which signals to me that I am doing something stupid.
Bottom line, is there an efficient way to look up the Q code for a "female given name"?

Related

Return a table of basic physical objects?

How can I return a table of basic physical objects, e.g., Ball (Q18545), Arrow (Q45922), in Wikidata using SPARQL?
I'm not able to directly return objects with the property Physical Object (Q223557) because it has way too many records. But its subtypes, e.g., Toy (Q11422) or Projectile (Q49393), are too narrow for me. I've tried the following to get my broad query working:
removing the label service
using LIMIT for a moderate number of records
filtering out records with very few sitelinks
limiting the objects to those with ids from BNCF Thesaurus ID, BabelNet ID, etc.
Nothing has worked for me. I suspect this is straightforward for anyone who's had more than a few days with Wikidata. Please help.
I shared my wrecked query below.
SELECT ?obj #?objLabel
WHERE {
{
SELECT ?obj WHERE {
?obj wdt:P508 ?bncfid;
wdt:P2581 ?bnid;
wdt:P227 ?gndid;
wdt:P8814 ?wsid;
wdt:P18 ?image;
wikibase:sitelinks ?sitelinks;
wdt:P31/wdt:P279* wd:Q223557.
#FILTER(?sitelinks > 5).
#FILTER(LANG(?objLabel)="en").
#SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
# ?obj rdfs:label ?objLabel}
}
LIMIT 1000
}
#?obj rdfs:label ?objLabel
#FILTER(LANG(?objLabel)="en").
}
(Run by clicking here)

Filter search in Wikidata API

I am trying to get filtered data from the Wikidata API, currently I can do a general search using this API, but now there have been specific cases where I have to filter this information, for example, I need to get a list of only authors to get the Q identifier and although I also reviewed the Wikidata Query Service this is too heavy to bring all the items, I used a SPARQL query and did a test and to get less than 3000 results it took 26 seconds, this is too much for a search service.
This is the query I use to get the authors.
SELECT DISTINCT ?author ?authorLabel WITH {
SELECT ?item ?author WHERE {
?item wdt:P50 ?author.
} LIMIT 100000
} AS %FOO {
INCLUDE %FOO
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
I also need to search by categories but it has not been possible for me to filter the searches in any way, does anyone know a way to do it?

On wikidata SPARQL get coordinates in degree?

I want to get coordinates via SPARQL from wikidata displayed as degrees (e.g. 54°54'36"N). It is displayed in wikidata like this, so I suspect there is a built-in function for this purpose but I can not find it.
Example query:
SELECT DISTINCT ?countryLabel ?long
{
?country wdt:P31 wd:Q6256 ;
p:P1332 [ psv:P1332 [wikibase:geoLatitude ?long ]].
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
This gives the longitude as number (e.g. -30.08). I can calculate the desired output format from this result but would prefer to get it directly from the query.
Thanks.

Efficient filter query in Wikidata

I am trying to form an efficient filter query in SPARQL on Wikidata. Let me explain my process:
I query the search-entities API using key words e.g. (Apple, Orange)
The API query returns a list of relevant item ID's e.g. (wd:Q629269, wd:Q154950, wd:Q312, wd:Q95, wd:Q4878289, wd:Q10817602)
With this list of ID's, I then query SPARQL and to return items that are CLASS or are SUBLCASS of certain types e.g. (p:P31/ps:P31/wdt:P279* wd:Q43229) - which returns everything if it is an Organisation or subclass thereof.
Then for items in the list of ID's, that are of certain CLASS, return information items if they exists e.g. (OPTIONAL).
I am new to SPARQL. My Question is, is this the most efficient method to achieve this output? It seems to me to be quite inefficient and I cannot find a similar type of problem in the tutorial examples.
You can try the query here.
SELECT distinct ?item ?itemLabel ?itemDescription ?web ?inception ?ISIN
WHERE{
FILTER (?item IN (wd:Q629269, wd:Q154950, wd:Q312, wd:Q95, wd:Q4878289, wd:Q10817602))
?item p:P31/ps:P31/wdt:P279* wd:Q43229.
OPTIONAL {
?item wdt:P856 ?web. # get item-web
?item wdt:P571 ?inception. # get item-web
?item wdt:P946 ?ISIN. # get item-isin
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
LIMIT 10

Wikidata sparql query returns 0 result

I’m new to query languages and linked data so thanks a lot for the help. I also have a similar question about sparql on dbpedia
dbpedia sparql query returns 0 result
I would like to look up all the art movements in wikidata with the associated artists (founder/inventor/creator, known for), date start, date end, country. Here is my query:
PREFIX wdno: <http://www.wikidata.org/prop/novalue/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?art ?artLabel ?start ?end ?countryLabel ?influencebyLabel WHERE {
?art wdt:P31 wd:Q968159 ;
wdt:P571 ?start ;
wdt:P576 ?end;
wdt:P17 ?country ;
wdt:P737 ?influenceby .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
When I run the query only for the artLabel, it shows 300s result but as only a few has start date, when I include the start date, my dataset shrank, and when I include the rest of the search terms, there’s few record that has all information. My question is how can I generate result where the empty cells also get recorded instead of discarded?
Also, what’s the difference between this wikidata result and the dbpedia result?
Thanks
Answered in the comments...
Ettore Rizza wrote:
You need to add optional clauses for the properties that are not always filled.