%META:TOPICPARENT{name="VirtTipsAndTricksGuide"}%
---+How Can I Use the SPARQL Pattern Matching Feature?
---++What?
The core feature of SPARQL logic of pattern matching: the same name in two different places of a
pattern which means that two fields of two matching triples should have same value.
---++Why?
Use the core feature of SPARQL logic of pattern matching in order to optimize your SPARQL queries for ex. by not
using the same variable inside and outside UNION
.
---++How?
A sample scenario demonstrating the usage of the core feature of SPARQL logic of pattern matching:
1 Assume the query:
SPARQL
PREFIX rdfs:
PREFIX dcterms:
PREFIX dgtwc:
PREFIX foaf:
PREFIX dbpedia:
PREFIX conversion:
PREFIX upstream:
SELECT ( count(DISTINCT ?dataset) as ?numresult )
WHERE
{
GRAPH
{
?dataset a conversion:CatalogedDataset .
?dataset dcterms:title ?title .
?abstract void:inDataset .
?abstract upstream:most_recent_scrape ?versioned .
?dataset void:inDataset ?versioned .
{
{
?dataset dcterms:title ?title .
?title bif:contains '"healthcare"' .
}
UNION
{
?dataset dcterms:description ?description .
?description bif:contains '"healthcare"' .
}
UNION
{
?dataset dgtwc:keywords ?keywords .
?keywords bif:contains '"healthcare"' .
}
}
}
}
;
1 Using the core feature of SPARQL logic of pattern matching for "?title", the query will look like this:
SPARQL
PREFIX rdfs:
PREFIX dcterms:
PREFIX dgtwc:
PREFIX foaf:
PREFIX dbpedia:
PREFIX conversion:
PREFIX upstream:
SELECT *
WHERE
{
GRAPH
{
{
{
?dataset dcterms:title ?t .
?t bif:contains '"healthcare"' .
}
UNION
{
?dataset dcterms:description ?description .
?description bif:contains '"healthcare"' .
}
UNION
{
?dataset dgtwc:keywords ?keywords .
?keywords bif:contains '"healthcare"' .
}
}
?dataset a conversion:CatalogedDataset .
?dataset dcterms:title ?title .
?dataset void:inDataset ?versioned .
?abstract void:inDataset .
?abstract upstream:most_recent_scrape ?versioned .
}
}
* Note: The only "partial" exception is "OPTIONAL": the same name used outside and inside OPTIONAL means that
two fields of two matching triples should have same value if OPTIONAL triple exists at all, but it may
not exist and no matching will be required in that case.
1 The both queries from above are not semantically equivalent as if a ?dataset
had multiple
titles, the bif:contains
in the second query may match a ?t
value that
contains "healthcare"
while ?title
is bound to other values (potentially
not containing "healthcare"). However, consider two cases:
1. The ?dataset
has some dcterms:title
for which ?t bif:contains '"healthcare"'
.
In this case two other branches of UNION are not needed, ?dataset
is found anyway,
and additional ?dataset dcterms:title ?title
is needed only if you want to enumerate all
titles of the ?dataset
, otherwise ?t
is the most adequate because ?t bif:contains '"healthcare"'
already.
1. The ?dataset
does not have any dcterms:title
with the '"healthcare"' word,
but it contains some appropriate description or comment. In this case the original query gets
?title
not bound and thus the whole match for ?dataset
fails.
1 If you want to find all datasets that have '"healthcare"' in any of three fields but prefer to return title
that contains '"healthcare"' rather than some other title then the query should look like:
SELECT ( COALESCE (?good_title, ?some_title))
...
WHERE
{
{
?dataset dcterms:title ?good_title .
?good_title bif:contains '"healthcare"' .
}
UNION
{
?dataset dcterms:description ?description .
?description bif:contains '"healthcare"' .
}
UNION
{
?dataset dgtwc:keywords ?keywords .
?keywords bif:contains '"healthcare"' .
}
}
...
?dataset dcterms:title ?some_title
...
1 Another variant is also:
SELECT (COALESCE (?good_title,
(
SELECT ?some_title ...
WHERE
{
?dataset dcterms:title ?some_title
}
) ) )
...
WHERE
{
{
?dataset dcterms:title ?good_title .
?good_title bif:contains '"healthcare"' .
}
UNION
{
?dataset dcterms:description ?description .
?description bif:contains '"healthcare"' .
}
UNION
{
?dataset dgtwc:keywords ?keywords .
?keywords bif:contains '"healthcare"' .
}
}
...
* In this case . two independent variables are passed to either SPARQL 1.1.
COALESCE()
or SPARQL-BI <bif:coalesce>()
built-in function,
depending on features supported by the server. That's the simplest way to "prioritize" values.
1 In addition, the query can be accelerated by the following trick: make ?catalog_title
and
?catalog_subtitle
OPTIONAL and thus postponed to the end even if grouping is required for
these two specific properties. So the fixed query will look like:
SPARQL
PREFIX rdfs:
PREFIX dcterms:
PREFIX dgtwc:
PREFIX foaf:
PREFIX dbpedia:
PREFIX conversion:
PREFIX upstream:
PREFIX void:
PREFIX xsd:
SELECT (?catalog_title AS ?id) (?catalog_subtitle AS ?label)
(COUNT(DISTINCT ?dataset) AS ?count)
WHERE
{
GRAPH
{
{
{
?dataset dcterms:title ?title .
?title bif:contains '"health"' .
}
UNION
{
?dataset dcterms:description ?description .
?description bif:contains '"health"' .
}
UNION
{
?dataset dgtwc:keywords ?keywords .
?keywords bif:contains '"health"' .
}
}
[ void:subset ?versioned ] void:inDataset .
?dataset void:inDataset ?versioned .
?dataset a conversion:CatalogedDataset .
OPTIONAL {
?dataset dgtwc:catalog_title ?catalog_title .
?dataset dgtwc:catalog_subtitle ?catalog_subtitle .
}
FILTER(bound (?catalog_title)) .
FILTER(bound (?catalog_subtitle)) .
}
}
GROUP BY ?catalog_title ?catalog_subtitle
;
---++Related
* [[VirtTipsAndTricksGuide][Virtuoso Tips and Tricks Collection]]
* [[http://docs.openlinksw.com/virtuoso/rdfsparql.html][Virtuoso Documentation]]