Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I limit the number of results for a specific variable in a SPARQL query?

Let's say I have a SPARQL query like this, looking for resources that have some shared property with a focal resource, and also getting some other statements about the focal resource :

CONSTRUCT {
  ?focal pred:icate ?shared .
  ?other pred:icate ?shared .
}
WHERE {
  ?focal pred:icate ?shared ;
         more:info ?etc ;
         a "foobar" .
  ?other pred:icate ?shared .
}
LIMIT 500

If there are more than 500 other resources, that LIMIT might exclude that more:info statement and object. So, is there a way to say "I only want at most 500 of ?other", or do I have to break this query into multiple pieces?

like image 275
treat your mods well Avatar asked Jan 17 '12 16:01

treat your mods well


2 Answers

You can use LIMIT in subqueries, i.e. something like the following:

CONSTRUCT {
  ?focal pred:icate ?shared .
  ?other pred:icate ?shared .
}
WHERE {
    ?focal pred:icate ?shared ;
           more:info ?etc ;
           a "foobar" .
    { 
      SELECT ?shared {
        ?other pred:icate ?shared .
      }
      LIMIT 500
    }
}
like image 134
Jan Avatar answered Nov 19 '22 05:11

Jan


http://www.w3.org/TR/2012/WD-sparql11-query-20120105/#modResultLimit

The LIMIT clause puts an upper bound on the number of solutions returned. If the number of actual solutions, after OFFSET is applied, is greater than the limit, then at most the limit number of solutions will be returned.

You can only limit the number of solutions to your query, not a specific subset of it. You can use a subquery with a LIMIT clause though: http://www.w3.org/TR/sparql-features/#Subqueries.

like image 1
Savino Sguera Avatar answered Nov 19 '22 05:11

Savino Sguera