Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adorsk-whoi/1031428 to your computer and use it in GitHub Desktop.
Save adorsk-whoi/1031428 to your computer and use it in GitHub Desktop.
SPARQL: Multiple facets in one query
# This SPARQL query could be used to calculate facets for SPARQL result set.
# The base result is defined as a subquery.
# Facets are calculated for the result set, and are returned as rows consisting of a facet id, a label,
# and a count.
# This query works against Virtuoso SPARQL endpoints.
# You can try it the against dbpedia virtuoso sparql endpoint, but it will probably fail due to the DBPedia endpoint's
# performance limitations.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbo: <http://dbpedia.org/ontology/>
# select the facet key, the facet value, and the facet count
SELECT distinct ?key ?facet_label ?facet_count
WHERE
{
{ # facets
{ # position facet
SELECT distinct ('position') as ?key ?position as ?facet_label count(distinct ?s) as ?facet_count
WHERE{
{?s dbo:position ?position} .
}
} # end position facet
UNION
{ # country facet
SELECT distinct ('country of birth') as ?key ?country as ?facet_label count(distinct ?s) as ?facet_count
WHERE{
{?s dbp:countryofbirth ?country} .
}
} # end country facet
# ... other facets ...
} # end facets
.
# subquery for base set for which facet counts will be calculated
{
SELECT distinct ?s
WHERE
{
{<http://dbpedia.org/resource/Arsenal_F.C.> dbp:name ?s} .
{?s rdf:type dbo:Person}.
}
} # End base set selection
} # End
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment