Why does this Wikidata query return no results? - wikipedia-api

Shouldn't this Wikidata query return something?
https://www.wikidata.org/w/api.php?action=query&titles=Kevin_Bacon
What am I missing?

Not, it shouldn't, there is no Wikidata page titled "Kevin Bacon". The Wikidata page containing information about the actor Kevin Bacon is https://www.wikidata.org/wiki/Q3454165.
This means that the API query https://www.wikidata.org/w/api.php?action=query&titles=Q3454165 will return something (specifically, the page id). Without knowing what do you actually want, it's hard to help you further, though it's likely the Wikidata-specific API methods are what you want, instead of action=query.

Related

Is it possible to find the Wikipedia ID by property, e.g. Grid ID

Wikidata stores the Grid ID of educational institutions in property 2427 (https://www.wikidata.org/wiki/Property:P2427). Is it possible to retrieve the Wikipedia ID for a given Grid ID.
Example: I know the value "grid.1012.2" as Grid ID.
I would like to get Q1517021 back!
I tried
https://www.wikidata.org/w/api.php?action=wbgetclaims&format=json&property=P2427&claim=grid.1012.2
without success...
You can use the search API with the haswbstatement keyword (docs): action=query&format=json&list=search&srsearch=haswbstatement%3AP2427%3Dgrid.1012.2
Although as suggested by others using SPARQL might be easier for these kinds of things.

Getting search results from wikidata website, but not API

I'm trying out the wikidata API but have some trouble with the search query "Jas 39 C Gripen". It returns results on the wikidata website, but not if I use the API.
On The wikidata website I get two search results for the query
https://www.wikidata.org/w/index.php?search=Jas+39+C+Gripen&title=Special:Search&fulltext=1
The same query using the API, does not return a result
https://www.wikidata.org/w/api.php?action=wbsearchentities&format=json&language=en&type=item&continue=0&search=Jas%2039%20C%20Gripen
Am I missing some parameters or using the wrong parameters? For many other queries I get results from the API.
It seems that the native Wikidata search applies some "fuzzy logic" when interpreting the user's entries. In your case, it shows two results, although the character C is missing in the first one.
Coming back to the API and the action you have chosen, you could use Jas 39 Gripen as search term (which will show one result) as well as Jas 39C Gripen (which will also show one result). But it seems that you can't use Jas 39 C Gripen (note the space character between 9 and C).
In other words,
https://www.wikidata.org/w/api.php?action=wbsearchentities&format=json&language=en&type=item&continue=0&search=Jas%2039%20Gripen
https://www.wikidata.org/w/api.php?action=wbsearchentities&format=json&language=en&type=item&continue=0&search=Jas%2039C%20Gripen
both work, but
https://www.wikidata.org/w/api.php?action=wbsearchentities&format=json&language=en&type=item&continue=0&search=Jas%2039%20C%20Gripen
does not.
I have investigated this issue further and finally found the solution. Try this:
https://www.wikidata.org/w/api.php?action=query&list=search&format=json&srsearch=Jas+39+C+Gripen
The action query allows some "fuzziness" in the search term. Please refer to the API documentation for further details. In short, this action performs a full text search (which you obviously want) and allows for a nearmatch search type.
The reason seems to be that the English label is JAS 39C Gripen. By removing one space from your query, you will get the result you are looking for:
https://www.wikidata.org/w/api.php?action=wbsearchentities&format=json&language=en&type=item&continue=0&search=Jas%2039C%20Gripen

How to get information of a specific Wikidata ID using SPARQL query?

I know how to find all movies in Wikidata. So, I get a list of IDs like "Q18644475". If I try to get all film info for every film in the database in one query, I get a timeout. So, I decided to get the film info in individual queries.
Is there an SPARQL query to get the title and release year(optionally all possible info) for this ID?
I find the hoops you have to jump through to use an entity ID in SPARQL to be quite nuts.
If anyone else stumbles across this question then an alternative is to download the complete entity information in JSON format.
For example:
https://www.wikidata.org/wiki/Special:EntityData/Q18644475.json

Freebase API - listing a city's tourist attractions by relevance

I'm trying to use Freebase to list tourist attractions for cities by relevance.
Using the Topic API, it's simple to retrieve results for a certain city using its MID (e.g. "/m/04jpl" for London)
https:// www.googleapis.com/freebase/v1/topic/m/04jpl/?&filter=/travel/travel_destination/tourist_attractions
However, this gives a limited 10 results. The response ends with "count": 87.0". How do I get all 87? It's possible to click a "87 values total" link on London's Freebase page. Effectively, I want to do the same here.
I realise I could use MQL, but I want the results to be ranked by relevance, not by timestamp. Using the Search API, it's possible to rank by freebase, entity or schema, so I'd rather use that.
First, I looked at the Search Output schema for the Search API. However, even outputting "all" didn't produce Tourist Attraction results. Using metaschema with the Search API DID work. I used "part_of" to select London. However, it only works for some locations:
https:// www.googleapis.com/freebase/v1/search?limit=50&filter=(all%20type:/travel/tourist_attraction%20part_of:/m/04jpl)&indent=true
What I REALLY want to be able to do is make it work for a relatively unknown location like "Loughborough" (MID /m/01z21p). As you can see, substituting /m/04jpl for /m/01z21p produces no results:
https:// www.googleapis.com/freebase/v1/search?limit=50&filter=(all%20type:/travel/tourist_attraction%20part_of:/m/01z21p)&indent=true
Looking at "Loughborough", we see that its tourist attraction like "Loughborough Town Hall" has a "/travel/tourist_attraction/near_travel_destination" of "Loughborough". How would I compose this filter?
I want something like the following (that actually works):
https:// www.googleapis.com/freebase/v1/search?limit=50&filter=(all%20type:/travel/tourist_attraction)&filter=(/travel/tourist_attraction/near_travel_destination:/m/01z21p)&indent=true
Thanks!
NOTE: To enter the links into your browser you need to remove the space between the https:// and www. I would have done so, but I don't have the required permissions here yet to post more than 2 links.
I solved this problem using 2 Freebase API calls.
1) An MQL query that gets a list of all the tourist attractions for a particular MID. These results are not ranked in any useful way. I am also returning the result number to make processing a little easier later
https://www.googleapis.com/freebase/v1/mqlread?query={"mid":"/m/04jpl","/travel/travel_destination/tourist_attractions":[{"mid":null}],"resultnumber:/travel/travel_destination/tourist_attractions":[{"return":"count"}]}
The list of returned MIDs are then used to create a new query (using a for loop). You must enter all MIDs returned from the above query, so that they can all be ranked together.
2) https://www.googleapis.com/freebase/v1/search?limit=10&filter=(any%20mid:/m/0gsxw%20mid:/m/01d_0p%20mid:/m/07gyc)&scoring=entity
It's best to choose a return format that just returns MIDs, to ensure that loading times aren't extensive.
You then have a ranked list of MIDs! You'll need one final query to return whatever details you desire.
I hope this has proved helpful.

SPARQL - learning by example

In short, if someone instructs me, "Get a list of databases which include Oracle, Mysql, DB2, MSSQL, etc.," how can I actually establish which fields and information I need to create the query? I downloaded DBpedia and grepped the triples, e.g.,
$ `grep 'Oracle' * | grep 'Database'
article_categories_en.nt:<http://dbpedia.org/resource/Oracle_Database> <http://purl.org/dc/terms/subject> <http://dbpedia.org/resource/Category:Relational_database_management_systems> .`
How do I create a SPARQL query from that initial question?
Using that snippet above, I know Oracle is a company and one of its products is an RDBMS by the same name. I tried the lookup tool and searching the web but did not get far enough with this and need some direction.
In this particular case, I'd start by visiting the DBpedia page corresponding to one of the things that you know you'd like in your result. A good instance is http://dbpedia.org/page/MySQL. On that page, you can start to look for properties that might help you find other related systems. One property listed on the page is
dbpedia-owl:genre dbpedia:Relational_database_management_system
Then I'd go the DBpedia SPARQL endpoint and try searching for other things that have this same property and value. In this case, I can run the following query:
SELECT * WHERE {
?rdbms dbpedia-owl:genre dbpedia:Relational_database_management_system .
}
SPARQL results
The results are pretty promising; there are 38 results. The first few are:
rdbms
http://dbpedia.org/resource/MySQL_Cluster
http://dbpedia.org/resource/MSQL
http://dbpedia.org/resource/OpenEdge_Advanced_Business_Language
http://dbpedia.org/resource/Adaptive_Server_Enterprise
…
In general, I find it much easier to locate an instance of the sort of thing that I'm looking for in Wikipedia, then visit the corresponding DBpedia page and look at the properties, and then formulate some exploratory queries based on that.