Ask about raw queries es on graphdb - 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 .
}

Related

How to get all people and their name and wikipedia link from DBPedia SparQL?

I am getting these results:
[
...
{
resource: {
type: 'uri',
value: 'http://dbpedia.org/resource/Benjamin_Swan_(Vermont)'
},
name: {
type: 'literal',
'xml:lang': 'en',
value: 'Balogun Yakub Abiodun'
}
},
{
resource: {
type: 'uri',
value: 'http://dbpedia.org/resource/Benjamin_Swan_(Vermont_politician)'
},
name: {
type: 'literal',
'xml:lang': 'en',
value: 'Balogun Yakub Abiodun'
}
},
{
resource: {
type: 'uri',
value: 'http://dbpedia.org/resource/Benjamin_Swearer'
},
name: {
type: 'literal',
'xml:lang': 'en',
value: 'Balogun Yakub Abiodun'
}
},
{
resource: {
type: 'uri',
value: 'http://dbpedia.org/resource/Benjamin_Sweet'
},
name: {
type: 'literal',
'xml:lang': 'en',
value: 'Balogun Yakub Abiodun'
}
}
...
]
For this query:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT ?resource ?name
WHERE {
?resource rdf:type dbo:Person .
?person a dbo:Person .
?person dbp:name ?name .
FILTER (LANG(?name) = 'en') .
}
offset 0
limit 50
The resource is linking to an English-named resource, yet the name returned is not English, even though marked as English. How do I just get the people names and their wikipedia links? It also appears I am requesting two separate things perhaps... How would you get their photo as well?
This is what I've ended up with but it seems to be returning things like List of Scottish X, and other non-people links.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT ?resource ?name ?depiction
WHERE {
?resource rdf:type dbo:Person .
?resource dbp:name ?name .
?resource foaf:depiction ?depiction .
FILTER (LANG(?name) = 'en') .
}
order by ?name
offset 0
limit 50

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 CONSTRUCT: restrict scope of BIND variable?

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?

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.

Make SPARQL queries for reviews

i'm trying to find the way to collect all the things that have this property:
"http://purl.org/stuff/rev#hasReview"
So I tryed to do this query on the
http://sparql.sindice.com/ endpoint:
PREFIX rev: <http://purl.org/stuff/rev#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT *
WHERE
{
?thing rev:hasReview ?review .
}
And the result was just a few, I think that if you don't provide the uri graph, it will search on his own graph. Is it on that way?
And my other doubt is, how can I know which is the graph uri from http://revyu.com/ for example?
Thanks.
The following query gets the graph:
PREFIX rev: <http://purl.org/stuff/rev#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT * WHERE {
{ GRAPH ?graph { ?thing rev:hasReview ?review . } }
}
In case you also want to get the triples in the default graph, you can use:
PREFIX rev: <http://purl.org/stuff/rev#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT * WHERE {
{ GRAPH ?graph { ?thing rev:hasReview ?review . } }
UNION
{ ?thing rev:hasReview ?review . }
}