Search code examples
sparqlontologydbpediasparqlwrapper

How to query DBPEDIA SPARQL to include the desired values while using LIMIT


I am trying to populate my local ontology with 10 random books. I would like to include 2 known books in the list for testing purpose.i.e out of the 10 I want to specify the names of 2 books and the remaining 8 can be random.

I do not know how to combine FILTER and LIMIT together.

I want the 2 books in the query along with 8 random books.

FILTER (str(?name) IN ("Panther in the Basement", "Endless Night"))

My code

SELECT  ?book ?date
WHERE {
?book rdf:type dbo:Book .
?book foaf:name ?name .
     ?book dbp:releaseDate ?date .

}
limit 10

Solution

  • It's not clear why you want to do this, but it can be done by a more complex query which is the UNION of

    1. the data of 8 other books
    2. the data for the two given books
    SELECT DISTINCT  ?book ?date
    WHERE
      {   { SELECT DISTINCT  ?book ?date
            WHERE
              { ?book  rdf:type         dbo:Book ;
                       foaf:name        ?name ;
                       dbp:releaseDate  ?date
                FILTER ( ?book NOT IN (dbr:Panther_in_the_Basement, dbr:Endless_Night) )
              }
            LIMIT   8
          }
        UNION
          { VALUES ?book { dbr:Panther_in_the_Basement dbr:Endless_Night }
            ?book  rdf:type         dbo:Book ;
                   foaf:name        ?name ;
                   dbp:releaseDate  ?date
          }
      }
    

    Note, since the DBpedia dataset is not that clean and has some books with multiple values for releaseDate this query might return duplicate books. To overcome this, you have to use GROUP BY + SAMPLE (or GROUP_CONCAT)

    Actually, I'd think that a more compact version of this query should also work, but it doesn't return any results on DBpedia:

    SELECT DISTINCT  ?book ?date
    WHERE
      { ?book  foaf:name        ?name ;
               dbp:releaseDate  ?date
          { { SELECT  ?book
              WHERE
                { ?book  rdf:type  dbo:Book }
              LIMIT   8
            }
          }
        UNION
          { VALUES ?book { dbr:Panther_in_the_Basement dbr:Endless_Night } }
      }