Search code examples
javasparqlsemantic-webdbpedia

How to query a particular DBpedia Resource/Page for multiple entities?


I have links to a number of DBpedia pages like:
http://dbpedia.org/resource/Harry_Potter
http://dbpedia.org/resource/Twilight_(series)
http://dbpedia.org/resource/Bible
http://dbpedia.org/resource/Manga

I would like to get the Abstract and Thumbnail entities for each one of them.

I can get them individually using:

  • For Abstract:

    PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX res: <http://dbpedia.org/resource/>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    SELECT ?label
    WHERE { <http://dbpedia.org/resource/Harry_Potter>
              dbo:abstract ?label . FILTER (lang(?label) = \'en\')}
    
  • For Thumbnail:

    PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX res: <http://dbpedia.org/resource/>
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    SELECT ?thumbnail
    WHERE { <http://dbpedia.org/resource/Harry_Potter>
              dbo:thumbnail ?thumbnail}
    

Is it possible to combine the above two queries into a single one. I am very new to SPARQL and couldn't get it to work.

Additionally, is there a better way to query than my current approach?


Solution

  • Of course it's possible to combine them, the trivial way to do it would be just to concatenate the bodies of the two WHEREs and adjust the SELECT accordingly:

    SELECT ?label ?thumbnail
    WHERE {
        <http://dbpedia.org/resource/Harry_Potter> dbo:abstract ?label . 
        FILTER (lang(?label) = 'en')
        <http://dbpedia.org/resource/Harry_Potter> dbo:thumbnail ?thumbnail .
    }
    

    If you want to be more succinct, you can combine two triples with the same subject using ;:

    SELECT ?label ?thumbnail
    WHERE {
        <http://dbpedia.org/resource/Harry_Potter>
            dbo:abstract ?label ; 
            dbo:thumbnail ?thumbnail .
        FILTER (lang(?label) = 'en')
    }
    

    And since you defined the res: prefix, you can use that to shorten the URI:

    SELECT ?label ?thumbnail
    WHERE {
        res:Harry_Potter
            dbo:abstract ?label ; 
            dbo:thumbnail ?thumbnail .
        FILTER (lang(?label) = 'en')
    }