6

I have this query. It matches anything which has "South" in its name. But I only want the one whose foaf:name is exactly "South".

SELECT Distinct ?TypeLabel 
WHERE
{
    ?a     foaf:name   "South" .
    ?a     rdf:type    ?Type .
    ?Type  rdfs:label  ?TypeLabel .
}
dsapalo
  • 1,819
  • 2
  • 19
  • 37
Tasawer Khan
  • 5,994
  • 7
  • 46
  • 69

3 Answers3

5

a bit late but anyway... I think this is what your looking for:

SELECT Distinct ?TypeLabel Where { 
?a foaf:name ?name . 
?a rdf:type ?Type . 
?Type rdfs:label ?TypeLabel . 
FILTER (?name="South"^^xsd:string)
}

you can use FILTER with the xsd types in order to restrict the result. hope this helps... cheers!

Lucas de Oliveira
  • 1,642
  • 11
  • 16
  • if i want to filter by type.. where do i have to insert the type (Class name) on this line? ?a rdf:type ?Type . Would it be this if my Class was of type Building: ?a rdf:type ?Building. – Metin Dagcilar Apr 08 '17 at 19:44
4

That query should match exactly the literal South and not literals merely containing South as a substring. For partial matches you'd go to FILTER with e.g. REGEX(). Your query engine is broken in this sense - which query engine you are working with?

laalto
  • 150,114
  • 66
  • 286
  • 303
  • I am using http://dbpedia.org/snorql. it breaks if we use regex. Given query does not match exactly. Try "Imran Khan" instead of "South" and display matching foaf:names it will also return results like "Imran Khan Niazy". – Tasawer Khan Apr 07 '10 at 13:03
  • 1
    as this is DBPedia it's running on Virtuoso under the hood and they no doubt have some of their free text search features turned on - it may not be standard SPARQL behaviour but Virtuoso's SPARQL implementation ain't exactly standard – RobV Apr 08 '10 at 16:13
4

(Breaking out of comments for this)

Data issues

The issue is the data, not your query. If use the following query:

SELECT DISTINCT ?a 
WHERE { 
    ?a foaf:name "Imran Khan" . 
}

You find (as you say) "Imran Khan Niazy". But looking at the dbpedia entry for Imran Khan, you'll see both:

foaf:name "Imran Khan Niazy"
foaf:name "Imran Khan"

This is because RDF allows repeated use of properties.

Cause

"South" had the same issue (album, artist, and oddly 'South Luton'). These are cases where there are both familiar names ("Imran Khan", "South"), and more precise names ("Imran Khan Niazy", "South (album)") for the purposes of correctness or disambiguation.

Resolution

If you want a more precise match try adding a type (e.g. http://dbpedia.org/ontology/MusicalWork for the album).

Beware

Be aware that DBpedia derives from Wikipedia, and the extraction process isn't perfect. This is an area alive with wonky data, so don't assume your query has gone wrong.

dsapalo
  • 1,819
  • 2
  • 19
  • 37
user205512
  • 8,798
  • 29
  • 28
  • 2
    What could be the reason that I get [No Result] for the same query [here](http://dbpedia.org/snorql/?query=SELECT+DISTINCT+%3Fa+%0D%0AWHERE+%7B+%0D%0A++++%3Fa+foaf%3Aname+%22Imran+Khan%22+.+%0D%0A%7D) – LYu Apr 18 '18 at 06:32