I have this query
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbpedia_property: <http://dbpedia.org/property/>
PREFIX dbpedia_ontology: <http://dbpedia.org/ontology/>
PREFIX yago: <http://dbpedia.org/class/yago/>
PREFIX schema: <http://schema.org/>
SELECT * WHERE
{
{
SELECT ?school, ?name, ?snippet, ?url, ?pageid, ?alias_1, ?alias_2, ?alias_3
WHERE
{
{ ?school rdf:type schema:EducationalOrganization . }
UNION
{ ?school rdf:type yago:EducationalInstitution108276342 . }
?school rdfs:label ?name .
OPTIONAL {
?school foaf:isPrimaryTopicOf ?url .
}
OPTIONAL {
?school dbpedia_ontology:wikiPageID ?pageid .
}
OPTIONAL {
?school rdfs:comment ?snippet .
FILTER (langMatches(lang(?snippet),"en"))
}
OPTIONAL {
?school dbpedia_property:name ?alias_1 .
FILTER ( langMatches(lang(?alias_1),"en") )
}
OPTIONAL {
?school foaf:name ?alias_2 .
FILTER ( langMatches(lang(?alias_2),"en") )
}
OPTIONAL {
?school dbpedia_ontology:wikiPageRedirects ?temp .
?temp rdfs:label ?alias_3 .
FILTER ( langMatches(lang(?alias_3),"en") )
}
OPTIONAL {
?school rdf:type ?excluded .
FILTER (?excluded = schema:Library)
}
FILTER ( langMatches(lang(?name),"en") && !BOUND(?excluded) )
}
ORDER BY ?name
}
}
LIMIT 1
OFFSET 0
You can see that the result gives the resource
http://dbpedia.org/resource/"Wesleyan_Methodist_College"
This will be redirected to
http://dbpedia.org/page/Southern_Wesleyan_University
Why doesn't Virtuoso resolve the resource and give the final destination?
Is there a way to instruct it to ignore the redirects?
The /resource/ and the /page/ about the resource are different things. One has a length in bytes, for example.
A web page is not an schema:EducationalOrganization.
If you look up with HTTP the /resource/, DBpedia sends back an HTTP 303 which a browser will then follows. That's your browser's choice.
See the output from:
wget --max-redirect 0 -O/dev/null -S http://dbpedia.org/resource/Wesleyan_Methodist_College
or
curl -v --max-redirs 0 http://dbpedia.org/resource/Wesleyan_Methodist_College
Related
Im trying to query a knowledge graph and im trying print the max occurrence of ?n in the result and i have tried running following query but it just doesn't prints anything
here is my SPARQL Query
PREFIX : <http://www.tafsirtabari.com/ontology#>
PREFIX RDF:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
select
?n
(MAX( xsd:int(?countOfSharedLikedItems)) as ?max)
(COUNT(?n) as ?countOfSharedLikedItems)
where {
?h :hasTheme :lugha .
?h RDF:type :Hadith .
?h :hasHadithNo ?o.
?p :isPartOfHadith ?h.
{
?p :hasNarratorSegment ?nc.
?nc :refersTo+/:hasName ?n.
}
Union
{
?p :hasRootNarratorSegment ?rnc.
?rnc :refersTo+/:hasName ?n.
}
}
i have also tried following by using group by ?n
PREFIX : <http://www.tafsirtabari.com/ontology#>
PREFIX RDF:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select
(MAX(?countOfSharedLikedItems) as ?max)
(COUNT(?n) as ?countOfSharedLikedItems)
where {
?h :hasTheme :lugha .
?h RDF:type :Hadith .
?h :hasHadithNo ?o.
?p :isPartOfHadith ?h.
{
?p :hasNarratorSegment ?nc.
?nc :refersTo+/:hasName ?n.
}
Union
{
?p :hasRootNarratorSegment ?rnc.
?rnc :refersTo+/:hasName ?n.
}
} group by ?n
You can try this
PREFIX : <http://www.tafsirtabari.com/ontology#>
select (COUNT(?o ) AS ?triples) where {
?k :heardFrom ?o
}
6. Which RAWI narrated most hadiths about TOPIC_A
PREFIX hash: <http://www.tafsirtabari.com/ontology#>
PREFIX W3:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX : <http://www.tafsirtabari.com/ontology#>
SELECT ?total WHERE{
select DISTINCT ?n (COUNT(?n) as ?total) where {
?commentary hash:mentions hash:اهل_المعرفه .
?segment hash:containsCommentary ?commentary.
?segment ?Fr ?h .
?h W3:type hash:Hadith.
?p :isPartOfHadith ?h.
{
?p :hasNarratorSegment ?nc.
?nc :refersTo+/:hasName ?n.
}
Union
{
?p :hasRootNarratorSegment ?rnc.
?rnc :refersTo+/:hasName ?n.
}
}GROUP BY ?n
}ORDER BY DESC(?total)
LIMIT 1
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
Here is my query:
PREFIX : <http://example.org/ns#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?id ?name (count(?s) as ?count)
WHERE {
?t a :Tag ;
:hasId ?id ;
:hasName ?name
OPTIONAL { ?s :hasTag ?t ;
rdf:type ?type }
FILTER (?type in (:Client, :Project, :Staff))
} GROUP BY ?id ?name
Tags with no objects are not included in the result. How can I get them also without using union?
Performance is also important
The goal is to gather information about tags (id, name) and number of objects they assign to (if no such objects count must be 0). Tag data sample:
:tag912 :hasId "912"^^xsd:integer
:tag912 :hasName "Phones"
This tag is assigned to 6 objects.
This query works for me:
PREFIX : <http://example.org/ns#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?id ?name ?count
WHERE {
{
SELECT ?id ?name (count(?s) as ?count)
WHERE {
?t a :Tag ;
:hasId ?id ;
:hasName ?name .
?s :hasTag ?t ;
rdf:type ?type
FILTER (?type in (:Client, :Project, :Staff))
} GROUP BY ?id ?name
} UNION {
SELECT ?id ?name (0 as ?count)
WHERE {
?t a :Tag ;
:hasId ?id ;
:hasName ?name
FILTER not exists { ?s :hasTag ?t }
}
}
}
How can I use bindings here? Will it improve performance?
Thank you
Why not use UNION?
Aside from that, your query is focused on ?t tags and their ?id and ?name — so it's not surprising that results don't include ?s objects which lack ?t and hence lack any ?id and ?name…
I think this may get you going in the right direction —
PREFIX : <http://example.org/ns#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?id
?name
(count(?s) as ?count)
WHERE
{ ?s rdf:type ?type
FILTER ( ?type IN ( :Client, :Project, :Staff ) ) .
OPTIONAL { ?s :hasTag ?t .
?t rdf:type :Tag ;
:hasId ?id ;
:hasName ?name }
}
GROUP BY ?id ?name
You can check if ?s is bound in your filter:
PREFIX : <http://example.org/ns#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?id ?name (COUNT(?s) as ?count)
WHERE {
?t a :Tag ;
:hasId ?id ;
:hasName ?name .
OPTIONAL {
?s :hasTag ?t ;
rdf:type ?type .
}
FILTER (!BOUND(?s) || ?type in (:Client, :Project, :Staff))
} GROUP BY ?id ?name
I don't know if this is faster than a union (or a bunch of unions), however.
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 . }
}
I'd like to do something like
{
SELECT ?page, "A" AS ?type WHERE
{
?s rdfs:label "Microsoft"#en;
foaf:page ?page
}
}
UNION
{
SELECT ?page, "B" AS ?type WHERE
{
?s rdfs:label "Apple"#en;
foaf:page ?page
}
}
But this gives a syntax error. How can I union two select queries in SPARQL?
You can union them like this:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT * WHERE
{
{
SELECT ?page ("A" AS ?type) WHERE
{
?s rdfs:label "Microsoft"#en;
foaf:page ?page
}
}
UNION
{
SELECT ?page ("B" AS ?type) WHERE
{
?s rdfs:label "Apple"#en;
foaf:page ?page
}
}
}
(check with the SPARQL validator)
However I don't think you need sub queries at all for this case. For example:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?page ?type WHERE
{
?s foaf:page ?page .
{ ?s rdfs:label "Microsoft"#en . BIND ("A" as ?type) }
UNION
{ ?s rdfs:label "Apple"#en . BIND ("B" as ?type) }
}
Based on #user205512's answer, here's one that works on Virtuoso:
SELECT * {
?s foaf:page ?page .
{
SELECT ?page ("A" AS ?type) {
?s rdfs:label "Microsoft"#en;
foaf:page ?page
}
} UNION {
SELECT ?page ("B" AS ?type) {
?s rdfs:label "Apple"#en;
foaf:page ?page
}
}
}
The trick was just do add an additional ?s foaf:page ?page triple outside of the UNION. This is obviously redundant, but it seems to avoid the Virtuoso bug, which is apparently caused when you have a “naked” UNION with subqueries.