Basic Queries

Table of contents
  1. Basic Queries
    1. Select
    2. Count
    3. Filter
    4. Bind
    5. Distinct & Order by
    6. Random
    7. Union
    8. Pagination

Select

Use SELECT to define which data you want to have returned with your query.

for example

This query will return the first 1.000 titles of objects that are published in the linked data event streams from the five participating cultural heritage institutions.

PREFIX cidoc: <http://www.cidoc-crm.org/cidoc-crm/>

SELECT ?title
WHERE { 
  ?object cidoc:P102_has_title ?title.
} 

try live

To get the results from only one heritage institution, specify which linked data eventstream you want to query using FROM.

for example

This query will return the first distinct 1.000 titles of objects that are published in the linked data event stream from het Huis van Alijn.

PREFIX cidoc: <http://www.cidoc-crm.org/cidoc-crm/>

SELECT ?title FROM <http://stad.gent/ldes/hva> 
WHERE { 
  ?object cidoc:P102_has_title ?title.
}

try live

When querying for fields that are linked to thesaurus terms or persons and institutions from the agents list, the result will be a external link to the concept.

for example

This query will return the first 1.000 links to objectnames that are published in the CoGhent event streams.

PREFIX cidoc: <http://www.cidoc-crm.org/cidoc-crm/>

SELECT ?objectname 
WHERE { 
  ?object cidoc:P41i_was_classified_by ?identifier.
  ?identifier cidoc:P42_assigned ?objectname.
} 

try live

To obtain the label of the terms, use SKOS or RDFS.

for example

This query will return the first 1.000 labels of objectnames that are published in the CoGhent event streams.

PREFIX cidoc: <http://www.cidoc-crm.org/cidoc-crm/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

SELECT ?label 
WHERE { 
  ?object cidoc:P41i_was_classified_by ?identifier.
  ?identifier cidoc:P42_assigned ?objectname.
  ?objectname skos:prefLabel ?label
}

try live

In addition, it is also possible to query on specific terms or agents, using the link to the external thesauri

for example

This query will return the records with objectname collecting card that are published in the CoGhent event streams.

PREFIX cidoc: <http://www.cidoc-crm.org/cidoc-crm/>

SELECT ?object ?title
WHERE { 
  ?object cidoc:P102_has_title ?title.
  ?object cidoc:P41i_was_classified_by ?identifier.
  ## look up with concept from AAT describing playing cards
  ?identifier cidoc:P42_assigned <http://vocab.getty.edu/aat/300192646>.
}

try live

Count

for example

This query returns the number of titles that are published in the event stream of het Huis van Alijn.

PREFIX cidoc: <http://www.cidoc-crm.org/cidoc-crm/>

SELECT COUNT( DISTINCT ?title) FROM <http://stad.gent/ldes/hva> 
WHERE { 
  ?object cidoc:P102_has_title ?title.
}

try live

Filter

Use FILTER to further specify your result.

for example

This query will return objects with titles that have the word ‘Gent’ in them that are published in the event stream from het Huis van Alijn.

PREFIX cidoc: <http://www.cidoc-crm.org/cidoc-crm/>

SELECT ?title 
FROM <http://stad.gent/ldes/hva> 
WHERE { 
  ?object cidoc:P102_has_title ?title.
  FILTER (regex(?title, "Gent", "i"))
} 

try live

This filter will look for the value you present in the entire field. Which means sometimes you get too much results. To make sure you only get values that fully equal your search value, use ^ in front of your value and $ after your value.

for example

this query returns the titel and label for all records in the CoGent event streams with subject ‘koffie’. It excludes for example records with subject ‘koffiemolen’

PREFIX cidoc: <http://www.cidoc-crm.org/cidoc-crm/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

SELECT DISTINCT ?title ?label 
WHERE { 
       ?object cidoc:P102_has_title ?title.
       ?object cidoc:P128_carries ?carries.
       ?carries cidoc:P129_is_about ?about.
       ?about cidoc:P2_has_type ?type.
       ?type skos:prefLabel ?label.
       FILTER (regex(?label, "^koffie$", "i"))
} ORDER BY DESC(?object)

try live

Bind

To manipulate the data in your query, BIND can be used.

BIND & REPLACE

for example

this query returns the first 100 records with ‘Gent’ in the title of the event stream of het Huis van Alijn and replaces Gent with Ghent.

PREFIX cidoc: <http://www.cidoc-crm.org/cidoc-crm/>

SELECT ?newtitle
FROM <http://stad.gent/ldes/hva> 
WHERE { 
  ?object cidoc:P102_has_title ?title.
  FILTER (regex(?title, "Gent", "i"))
  BIND (REPLACE(str(?title), "Gent", "Ghent") AS ?newtitle).
} LIMIT 100

try live

BIND & CONCAT

for example

this query returns the link to the (different versions) of the event stream of het Huis van Alijn for a certain objectnumber (in filter).

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX adms: <http://www.w3.org/ns/adms#>
PREFIX prov: <http://www.w3.org/ns/prov#>
		
SELECT DISTINCT ?ldes FROM <http://stad.gent/ldes/hva> 
WHERE { 
  ?object adms:identifier ?identifier.
  ?identifier skos:notation ?objectnumber.
  FILTER (regex(?objectnumber, "2004-247-616", "i")).
  ?object prov:generatedAtTime ?time.
BIND(URI(concat("https://apidg.gent.be/opendata/adlib2eventstream/v1/hva/objecten?generatedAtTime=", ?time)) AS ?ldes)
} ORDER BY DESC(?object)

try live

Distinct & Order by

Because of the specifications of a linked data event stream, multiple versions of records can be present. When metadata changes, the linked data event stream will publish a new version of a record, but an older version of the record still remains. To obtain solely the latest version of records, use ORDER BY to order the records chronologically (most recent versions first) and DISTINCT (to drop the following versions).

for example

this query returns the first 1.000 records of the CoGhent event streams. It orders the records on publishing date. Second, it keeps only the first priref and drops any possible older versions.

PREFIX purl: <http://purl.org/dc/terms/>

SELECT DISTINCT ?priref
WHERE {
SELECT ?object ?priref
WHERE { 
?object purl:isVersionOf ?priref.
} ORDER BY DESC(?object)
}

try live

Random

You can randomize your result.

for example

this query returns one random IIIF manifest from the CoGhent event streams.

PREFIX cidoc: <http://www.cidoc-crm.org/cidoc-crm/>

SELECT ?o WHERE {
  ?s cidoc:P129i_is_subject_of ?o .
  BIND(RAND() AS ?random) .
} ORDER BY ?random
LIMIT 1

try live

The Coghent Random Image Viewer, for example, makes use of above query. The code can be found here

Union

To query multiple endpoint instead of all, it suffices to add the extra endpoint to the query.

for example

this query returns the first 1.000 titles from the event streams from Design Museum Gent and het Huis van Alijn.

PREFIX cidoc: <http://www.cidoc-crm.org/cidoc-crm/>

SELECT ?title 
FROM <http://stad.gent/ldes/hva> 
FROM <http://stad.gent/ldes/dmg> 
WHERE { 
  ?object cidoc:P102_has_title ?title.
} 

try live

However, when the query gets to complicated, it is better to use UNION.

for example

this query returns 100 records with creator ‘Joseph Buyens’ from the event streams of het Industriemuseum en Archief Gent.

PREFIX cidoc: <http://www.cidoc-crm.org/cidoc-crm/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX la: <https://linked.art/ns/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT DISTINCT ?institution ?title ?label
WHERE {
  ?object cidoc:P50_has_current_keeper ?institution .
  ?object cidoc:P102_has_title ?title .
  ?object cidoc:P108i_was_produced_by ?production .
  ?production cidoc:P14_carried_out_by ?maker .
  {?maker la:equivalent <https://stad.gent/id/agent/670003618> } UNION {?maker la:equivalent <https://stad.gent/id/agent/570025558>} .
  ?maker la:equivalent ?creator.
  ?creator rdfs:label ?label.
} LIMIT 100

try live

Pagination

the SparQL endpoint limits results to 1.000 lines. If a query has more than 1.000 results, multiple queries (each containing the next 1.000 results), using OFFSET and LIMIT, are necessary to obtain the entire result. You will need to count (see above) how many results your query gives first, to know how many pages to query.

for example

This query gets skips the first 1.000 results to return the next 1.000 results. It returns the records with subject circus from the collection of het Huis van Alijn.

PREFIX purl: <http://purl.org/dc/terms/>
PREFIX cidoc: <http://www.cidoc-crm.org/cidoc-crm/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

   SELECT DISTINCT ?priref ?label
   WHERE {
     SELECT ?object ?priref ?label FROM <http://stad.gent/ldes/hva>
     WHERE { 
     
       ?object purl:isVersionOf ?priref.
       ?object cidoc:P128_carries ?carries.
       ?carries cidoc:P129_is_about ?about.
       ?about cidoc:P2_has_type ?type.
       ?type skos:prefLabel ?label.

       FILTER (regex(?label, "^circus$", "i"))

     } ORDER BY DESC(?object)
  }
LIMIT 1000
OFFSET 1000

try live