SPARQL CONSTRUCT: restrict scope of BIND variable? - sparql

I am using Jena ARQ with this query:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX sc: <http://iiif.io/api/presentation/2#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX as: <http://www.w3.org/ns/activitystreams#>
CONSTRUCT {?collectionIRI rdf:type sc:Collection .
?collectionIRI as:items ?manifest .
?manifest rdf:type sc:Manifest}
WHERE {GRAPH ?g {?manifest rdf:type sc:Manifest .
BIND (URI(STR(CONCAT("https://api.repository.org/orgs/example.domain.org/collections/" , STRUUID()))) AS ?collectionIRI) .
FILTER(regex(str(?manifest),"example.domain.org"))
}}
The intention is to create a single ?collectionIRI by BINDing STRUUID() to a domain name (e.g. "example.domain.org" - a query parameter).
With this query every ?manifest in the result set is bound to a different ?collectionIRI and the result looks like this:
{
"#graph": [
{
"#id": "https://api.repository.org/orgs/example.domain.org/collections/0342aaf2-b772-48ea-be90-298a555ef5ab",
"#type": "sc:Collection",
"items": "http://example.domain.org/unescoCzechReformation/AIPDIG-NKCR__X_C_23______3CKR674-cs/"
},
{
"#id": "https://api.repository.org/orgs/example.domain.org/collections/04782c88-81cf-4d7b-8ac6-f15d83f094a5",
"#type": "sc:Collection",
"items": "http://example.domain.org/unescoCzechReformation/AIPDIG-NKCR__VIII_G_26___38E5KUD-cs/"
}
]
}
instead of the desired result:
{
"#graph": [
{
"#id": "https://api.repository.org/orgs/example.domain.org/collections/0342aaf2-b772-48ea-be90-298a555ef5ab",
"#type": "sc:Collection",
"items": [
"http://example.domain.org/unescoCzechReformation/AIPDIG-NKCR__VIII_B_6____30XYOX6-cs/",
"http://example.domain.org/unescoCzechReformation/AIPDIG-NKCR__I_E_45______2KDU8A5-cs/"
]
}
]
}
Is it possible to restrict BIND scope with CONSTRUCT?

Related

Ask about raw queries es on graphdb

I'm query by SPARQL like this
PREFIX inst: http://www.ontotext.com/connectors/elasticsearch/instance#
PREFIX : <http://www.ontotext.com/connectors/elasticsearch#>
PREFIX inst: <http://www.ontotext.com/connectors/elasticsearch/instance#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?entity {
?search a inst:test ;
:query '''{
{
"bool" : {
"should" : [ {
"query_string" : {
"query" : "Vie"
}
}]
}
}
}
''' ;
:entities ?entity .
}
and the connector and data
PREFIX : <http://www.ontotext.com/connectors/elasticsearch#>
PREFIX inst: <http://www.ontotext.com/connectors/elasticsearch/instance#>
INSERT DATA {
inst:test :createConnector '''
{
"elasticsearchNode": "localhost:9200",
"types": [
"http://test.com#Person"
],
"fields": [
{
"fieldName": "age",
"propertyChain": [
"http://test.com#age"
],
"analyzed": false
},
{
"fieldName": "learn",
"propertyChain": [
"http://test.com#learn"
],
"fielddata": true
}
]
}
''' .
}
Data
PREFIX test: <http://test.com#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
insert data {
test:5 rdf:type test:Person;
test:age 11112;
test:learn "Vie" .
}
But i always get Error 500
Query evaluation error: Invalid json for raw query given.
I have tried many times but cant query by raw query ES.
How can i raw query es on graphdbb. Thanks a lot.
There is a bug in documentation, that's why this error is thrown. I'll raise issue in GraphDB documentation. The provided example isn't valid JSON. Change query to:
PREFIX : <http://www.ontotext.com/connectors/elasticsearch#>
PREFIX inst: <http://www.ontotext.com/connectors/elasticsearch/instance#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?entity {
?search a inst:test ;
:query '''{
"query" : {
"bool" : {
"should" : [ {
"query_string" : {
"query" : "Vie"
}
}]
}
}
}
''' ;
:entities ?entity .
}

Returning nodes in a graph which do not have a rdf:type of rdfs:Class or rdf:Property

I am using the json-ld format.
Let say I have the following Data Graph
{
"#context": {
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"hr": "http://learningsparql.com/ns/humanResources#",
"d": "http://learningsparql.com/ns/data#",
"sh": "http://www.w3.org/ns/shacl#"
},
"#graph": [
{
"#id": "hr:Employee",
"#type": "rdfs:Class",
"rdfs:comment": "a good employee",
"rdfs:label": "model"
},
{
"#id": "hr:Another",
"#type": "rdfs:Class"
},
{
"#id": "hr:name",
"#type": "rdf:Property"
},
{
"#id": "hr:randomtype",
"#type": "hr:invalidtype",
"rdfs:comment": "some comment about randomtype",
"rdfs:label": "some label about randomtype"
},
{
"#id": "hr:typo",
"#type": "rdfs:Classs",
"rdfs:comment": "some comment about typo",
"rdfs:label": "some label about typo"
},
{
"#id": "hr:missing",
"rdfs:comment": "some comment about missing"
}
]
}
(ttl equivalent)
#prefix d: <http://learningsparql.com/ns/data#> .
#prefix hr: <http://learningsparql.com/ns/humanResources#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix sh: <http://www.w3.org/ns/shacl#> .
#prefix xml: <http://www.w3.org/XML/1998/namespace> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
hr:Another a rdfs:Class .
hr:Employee a rdfs:Class ;
rdfs:label "model" ;
rdfs:comment "a good employee" .
hr:missing rdfs:comment "some comment about missing" .
hr:name a rdf:Property .
hr:randomtype a hr:invalidtype ;
rdfs:label "some label about randomtype" ;
rdfs:comment "some comment about randomtype" .
hr:typo a rdfs:Classs ;
rdfs:label "some label about typo" ;
rdfs:comment "some comment about typo" .
I would like returned to me the nodes:
{
"#id": "hr:randomtype",
"#type": "hr:invalidtype",
"rdfs:comment": "some comment about randomtype",
"rdfs:label": "some label about randomtype"
}
and
{
"#id": "hr:typo",
"#type": "rdfs:Classs",
"rdfs:comment": "some comment about typo",
"rdfs:label": "some label about typo"
}
and
{
"#id": "hr:missing",
"rdfs:comment": "some comment about missing"
}
because in one case the type is invalid, another has a typo, and the last is missing the type information.
If only the "#id" information was returned, that would be sufficient.
What is the SPARQL query that returns this information?
If you want back all resources that do not have a type of either rdfs:Class or rdf:Property, then the query could be something like:
SELECT ?s
WHERE {
?s ?p ?o .
FILTER NOT EXISTS {
?s a ?c .
FILTER(?c IN (rdfs:Class, rdf:Property))
}
}

SPARQL query to access inside a JSON-LD array

Here is my JSON-LD :
"locationCreated": {
"#type": "Place",
"#id": "http://test.fr/city/pacific-grove",
"geo": {
"#type": "GeoCoordinates",
"#id": "http://test.fr/city/pacific-grove/geo",
"latitude": "36.6177374",
"longitude": "-121.9166215"
},
"name": "Pacific Grove",
"alternateName": "Pacific Grove, CA, USA"
}
I want to get the latitude & longitude value with SPARQL. Actually, when i make my query, I just have access to the URI "http://test.fr/city/pacific-grove" of locationCreated.
SPARQL Query:
prefix schema: <http://schema.org/>
select ?locationCreated
where {
?x schema:locationCreated ?locationCreated
}
LIMIT 100
Thank you for your answer.
This should work given your sample data:
prefix schema: <http://schema.org/>
select ?locationCreated ?lat ?long
where {
?x schema:locationCreated ?locationCreated .
?locationCreated schema:geo ?geoData .
?geoData schema:latitude ?lat .
?geoData schema:longitude ?long .
}
LIMIT 100

How to get Wikidata labels by ID using SPARQL?

How can I get the labels in English or any other language in Wikidata by ID using SPARQL endpoint?
Suppose wd:Q146190 is your wikidata entity ID
get the label in a specific language of your specific entity ID:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT *
WHERE {
wd:Q146190 rdfs:label ?label .
FILTER (langMatches( lang(?label), "EN" ) )
}
LIMIT 1
live example in english
live example in german
get all the labels in any language of your specific entity ID:
SELECT * WHERE {
wd:Q146190 rdfs:label ?label
}
here the link to the live try press on play to run the query then you could download a full JSON and get such a rensponse ..only a trunk copied here:
{
"head": {
"vars": [
"label"
]
},
"results": {
"bindings": [
{
"label": {
"xml:lang": "ar",
"type": "literal",
"value": "دوار الشمس الدرني"
}
},
{
"label": {
"xml:lang": "az",
"type": "literal",
"value": "Kökyumrulu günəbaxan"
}
},
..etc,etc.
get labels for each entity ID in a response resultset with multiple id
in this case you should use the Label service
example without labels
example with labels for each entity in the specified language: english
SELECT ?p ?pLabel ?w ?wLabel WHERE {
wd:Q30 p:P6/ps:P6 ?p .
?p wdt:P26 ?w .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
to use this service add Label to the variable ( ie: for ?p label you must use ?pLabel then add
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
to the WHERE block

Grouping SPARQL results for the same property?

I have the following SPARQL query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
SELECT ?subsidiary ?employees
WHERE {
<http://dbpedia.org/resource/Google> dbo:subsidiary ?subsidiary;
dbo:numberOfEmployees ?employees .
}
And I receive the following response:
{
"head": {
"vars": [ "subsidiary" , "employees" ]
} ,
"results": {
"bindings": [
{
"subsidiary": { "type": "uri" , "value": "http://dbpedia.org/resource/YouTube" } ,
"employees": { "datatype": "http://www.w3.org/2001/XMLSchema#integer" , "type": "typed-literal" , "value": "53861" }
} ,
{
"subsidiary": { "type": "uri" , "value": "http://dbpedia.org/resource/Motorola_Mobility" } ,
"employees": { "datatype": "http://www.w3.org/2001/XMLSchema#integer" , "type": "typed-literal" , "value": "53861" }
} ,
{
"subsidiary": { "type": "uri" , "value": "http://dbpedia.org/resource/Picnik" } ,
"employees": { "datatype": "http://www.w3.org/2001/XMLSchema#integer" , "type": "typed-literal" , "value": "53861" }
}
]
}
}
The number of employees is returned multiple times because their are multiple subsidiaries associated with the queried resource. Is it possible to collapse all these subsidiaries into a list and get something more like:
{
"head": {
"vars": [ "subsidiary" , "employees" ]
} ,
"results": {
"bindings": [
{
"subsidiary": { "type": "uri" , "value": [
"http://dbpedia.org/resource/YouTube",
"http://dbpedia.org/resource/Motorola_Mobility",
"http://dbpedia.org/resource/Picnik"
]
} ,
"employees": { "datatype": "http://www.w3.org/2001/XMLSchema#integer" , "type": "typed-literal" , "value": "53861" }
}
]
}
}
Not really. There isn't a an array datatype in the results format. You can create this association by processing the results, ORDER BY ?subsidiary ?employees will put the rows adjacent that you want to collapse.
There is GROUP_CONCAT but it produces a string which is the "," separated concat of string in a GROUP. It would been parsing. An engine may have extension aggregate functions.