sparqlwikidata

Search Item in Wikidata SPARQL by Title ignoring special characters, like regular search


I want to find a movie by a title regardless special characters that wiki has in it.

For example I am searching for "Guyver Dark Hero" movie.
Wiki has it under the title "Guyver: Dark Hero".

When I use this query it can't find any thing:

SELECT * WHERE {
  SERVICE wikibase:mwapi {       
    bd:serviceParam wikibase:api "EntitySearch" .       
    bd:serviceParam wikibase:endpoint "www.wikidata.org" .       
    bd:serviceParam mwapi:search "Guyver Dark Hero" .       
    bd:serviceParam mwapi:language "en" .       
    ?item wikibase:apiOutputItem mwapi:item .       
  } 
  {
    ?item wdt:P31 wd:Q11424;
          rdfs:label ?itemLabel.
  }
}
LIMIT 1

But if I search by "Guyver: Dark Hero", the item will be found.
How can I search just by any text that should have a match?
Like by:
Guyver Dark Hero
Guyver - Dark Hero
Guyver-Dark Hero

Please check query here.


Solution

  • If you want a more flexible search, you can use the Search service instead of EntitySearch (see MWAPI § Supported services):

    SELECT * WHERE {
      SERVICE wikibase:mwapi {
        bd:serviceParam wikibase:api "Search" .
        bd:serviceParam wikibase:endpoint "www.wikidata.org" .
        bd:serviceParam mwapi:srsearch "Guyver Dark Hero" .
        ?itemId wikibase:apiOutput mwapi:title .
      }
      BIND(URI(CONCAT(STR(wd:),?itemId)) AS ?item)
      {
        ?item wdt:P31 wd:Q11424;
              rdfs:label ?itemLabel.
      }
    }
    LIMIT 1
    

    The service's output is a page title (that on Wikidata is just an item ID), which I convert to an URI for making it referable by the other subquery.