Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify multiple rdf:types in a SPARQL query

Tags:

rdf

sparql

I have a SPARQL query like this

PREFIX prefix: <http://example.org/ns#>
SELECT *
WHERE 
{
    ?x rdf:type ?type .
}

Suppose now I want to specify the type of ?type as being either prefix:type1 or prefix:type2; how should this be done?

like image 777
Noor Avatar asked Apr 15 '13 22:04

Noor


People also ask

What types of queries does SPARQL support?

SPARQL contains capabilities for querying required and optional graph patterns along with their conjunctions and disjunctions. SPARQL also supports aggregation, subqueries, negation, creating values by expressions, extensible value testing, and constraining queries by source RDF graph.

What is the result type of an SPARQL ask query?

SPARQL also supports extensible value testing and constraining queries by source RDF graph. The results of SPARQL queries can be results sets or RDF graphs.

How do you write queries in SPARQL?

Structure of a SPARQL Query A SPARQL query comprises, in order: Prefix declarations, for abbreviating URIs. Dataset definition, stating what RDF graph(s) are being queried. A result clause, identifying what information to return from the query.

What is a SPARQL prefix?

"PREFIX", however (without the "@"), is the SPARQL instruction for a declaration of a namespace prefix. It allows you to write prefixed names in queries instead of having to use full URIs everywhere. So it's a syntax convenience mechanism for shorter, easier to read (and write) queries.


4 Answers

Much faster than the FILTER IN function is the use of BINDINGS. I would highly recommend using something along the lines of the following query rather than the FILTER(?type IN (wo:Kingdon, wo:Phylum).

SELECT * WHERE { ?x rdf:type ?type } BINDINGS ?type {(prefix:type1) (prefix:type2)}

Using BINDINGS allows the SPARQL engine to limit results as it is being processed rather than returning all results before filtering them. This makes returning the results much faster.

like image 191
BigDataBill Avatar answered Oct 04 '22 07:10

BigDataBill


You could use UNION e.g.

PREFIX prefix: <http://example.org/ns#>
SELECT *
WHERE {
    { ?x a prefix:type1 } UNION { ?x a prefix:type2 }
}

Note the use of a which is a SPARQL keyword that may be used in the predicate position and corresponds to the RDF type URI http://www.w3.org/1999/02/22-rdf-syntax-ns#type

There are other ways to do this such as using FILTER clauses with various expressions:

  • Series of ?type = prefix:type1 combined with the conditional or operator ||
  • ?type IN (prefix:type1, prefix:type2)

Or you could use a VALUES clause to specify the options for ?type

These may be better if your query is more complex and you don't want to duplicate much of the query onto both sides of the UNION or have more that two possibilites to consider.

like image 27
RobV Avatar answered Oct 04 '22 06:10

RobV


As Joshua mentioned, the accepted answer is no longer accurate in SPARQL 1.1.

In SPARQL 1.1, the VALUES keyword can be used but the syntax looks more complicated for use-cases like this one. However, you can write that in compact form as well:

SELECT * WHERE {
  VALUES ?type { prefix:Type1 prefix:Type2 }
  ?x rdf:type ?type
}
like image 24
Adrian Gschwend Avatar answered Oct 04 '22 08:10

Adrian Gschwend


You could do it using the FILTER syntax, like this:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX prefix: <http://example.org/ns#>

SELECT ?s ?p ?o ?type
WHERE {
   ?s a ?type .
   ?s ?p ?o .
  FILTER(?type IN (prefix:Type1, prefix:Type2))
}

Please note that I cannot guarantee efficiency, as I do not know if the the filtering will apply after all the results will be returned or not.

like image 34
Pantelis Natsiavas Avatar answered Oct 04 '22 08:10

Pantelis Natsiavas