Search code examples
pythonsparqldbpedia

SPARQL DBpedia query times out


I'm trying to execute a fairly simple SPARQL query on DBPedia from Python, as follows:

from SPARQLWrapper import SPARQLWrapper, JSON
city_name = 'Manhattan'
query = """select * 
               where {
               ?URI rdfs:label ?name.
               filter(regex(str(?name), "^%s"))
           }"""%(city_name)
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
sparql.setReturnFormat(JSON)
sparql.setQuery(query)
result = sparql.query().convert()

I want to retrieve all entities that match a given city in the first part of their name. I know that's a lot of entities but it executes fine in the DBPedia test browser here.

Whenever I try to run the above query in Python I end up with a timeout error:

EndPointInternalError: EndPointInternalError: endpoint returned code 500 and response. 

Response:
Virtuoso S1T00 Error SR171: Transaction timed out

Any advice on avoiding this timeout error? I realize that I might have to make my query more specific to tighten the bounds of the search.


Solution

  • Do a full text search first with bif:contains, and filter that afterwards:

    SELECT * {
      ?uri rdfs:label ?name .
      ?name bif:contains "Manhattan" . # Or "'Manhattan*'"
      FILTER(STRSTARTS(?name, "Manhattan"))
    }