SPARQL Examples

From Devwiki
Jump to: navigation, search

Example SPARQL Queries

Find PID for phaseCode

select ?pid from <#ri> where {
 ?pid <http://dev.llgc.org.uk/digitisation/identifiers/phaseCode> "ACCWP060" .
 } 

Find PID for original file name

select ?pid from <#ri> where {
?pid <http://dev.llgc.org.uk/digitisation/identifiers/original_name> "pjs00001" .
} 

Find PID for Project code

select ?pid from <#ri> where {
?pid <http://dev.llgc.org.uk/digitisation/identifiers/projectCode> "PJS" .
}

Find PID for Catalogue record

select ?pid from <#ri> where {
?pid <http://dev.llgc.org.uk/digitisation/identifiers/catalogue> "vtls004432228" .
}

These are mostly of use internally but may help others to see examples. These are mostly SPARQL 1.0 compliant as this is what Fedora 3.3 uses.


Find all Newspaper titles which aren't part of the Europeana colleciton Had to run two sparql queries and diff the results:

Query to get all Newspaper titles:

select ?title ?code  from <#ri> where {
   ?title <fedora-model:hasModel> <info:fedora/model:scif-newspapers-title> .
   ?title <http://dev.llgc.org.uk/digitisation/identifiers/nlw_id> ?code 
}order by ?code

Query to get all titles which are part of the europeana collection:

select ?title ?code  from <#ri> where {
   ?title <fedora-model:hasModel> <info:fedora/model:scif-newspapers-title> .
   ?title <http://dev.llgc.org.uk/digitisation/identifiers/nlw_id> ?code  .
   ?title <fedora-rels-ext:isMemberOfCollection> <info:fedora/collection:europeana-newspaper-title> 
}order by ?code

Query to find fedora sdefs and bdeps for pid:

select $model $sdef $sdep from <#ri> where{
  <info:fedora/llgc-id:2035358> <info:fedora/fedora-system:def/model#hasModel> $model .
  $model <info:fedora/fedora-system:def/model#hasService> $sdef .
  $sdep <info:fedora/fedora-system:def/model#isDeploymentOf> $sdef .
  $sdep <info:fedora/fedora-system:def/model#isContractorOf> $model
}

Query to get wiki data back for images:

SELECT ?object ?objectLabel ?handle ?dateCreated ?creator ?creatorLabel ?creatorID ?dipictsLabel ?coords
WHERE
{
   SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } 
   ?object wdt:P361 wd:Q21542493 .
   ?object wdt:P1184 ?handle .
   optional { 
      ?object wdt:P170 ?creator
      optional { ?creator wdt:P214 ?creatorID}
   }
   optional { ?object wdt:P571 ?dateCreated }
   optional { 
     ?object wdt:P180 ?dipicts  .
     ?dipicts wdt:P625 ?coords   
   } 
} order by ?handle

Note: label is created using fieldnameLabel the capital L is important in Label

Query to get back dipicts information including people and places:

SELECT ?object ?objectLabel ?handle ?id ?dateCreated ?creator ?creatorLabel ?creatorID ?typeLabel ?dipicts ?dipictsLabel ?coords
WHERE
{
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } 
  ?object wdt:P361 wd:Q21651637 .
  optional { ?object wdt:P1184 ?handle }
  optional { ?object wdt:P217 ?id }
  optional { 
     ?object wdt:P170 ?creator
     optional { ?creator wdt:P214 ?creatorID}
  }
  optional { ?object wdt:P571 ?dateCreated }
  optional { 
    ?object wdt:P180 ?dipicts  .
    ?dipicts wdt:P31 ?type
    optional {
    ?dipicts wdt:P625 ?coords
             }
  } 
} order by ?handle

--GlenRobson 10:18, 24 January 2017 (GMT)