Search code examples
sparqldbpedia

DBPedia Sparql with one request get links for multiple resources


Currently I'm doing for every entity that I have a single request to the sparql endpoint to get all the links for it e.g.

SELECT * WHERE {
   {<http://dbpedia.org/resource/San_Francisco> rdf:type ?link}
}

I want to make it more efficient, and I would like to know if there is a way to get the links for multiple entities with one request. I threw something together but this gives me just a big list with all the links.

SELECT * WHERE {
    {<http://dbpedia.org/resource/San_Francisco> rdf:type ?link}
    UNION
    {<http://dbpedia.org/resource/Silicon_Valley> rdf:type ?link}
}

Can I somehow get the links, so I can identify to which entity they belong?


Solution

  • You can be a little more succinct with the SPARQL 1.1 values keyword:

    SELECT  *
    WHERE
      { VALUES ?entity { <http://dbpedia.org/resource/San_Francisco>
            <http://dbpedia.org/resource/Silicon_Valley> }
        ?entity  rdf:type  ?link
      }
    

    Do you want a distinct list? Maybe sort the list and add English-language labels if available?

    SELECT DISTINCT  ?link ?llab
    WHERE
      { VALUES ?entity { <http://dbpedia.org/resource/San_Francisco>
            <http://dbpedia.org/resource/Silicon_Valley> }
        ?entity  rdf:type  ?link
        OPTIONAL
          { ?link  rdfs:label  ?llab
            FILTER ( lang(?llab) = "en" )
          }
      }
    ORDER BY ?link