问题
I have to perform some filtering in a turtle/n3 file, returning another file of the same kind. The basic data element (location) i work on is this:
:pt0001
vcard:category "Poste e Telegrafi"
; vcard:fn "Ufficio Bologna 1"
; vcard:extended-address "Via Cairoli 9, Bologna BO, Italy"
; vcard:latitude "44.504192"
; vcard:longitude "11.338661"
; vcard:tel "051 243425"
; vcard:fax "051 244459"
; cs:opening "Mon, Tue, Wed, Thu, Fri: 0800-1330. Sat: 0800-1230."
; cs:closing "01-01, 01-06, P, LA, 04-25, 05-01, 06-02, 08-15, 11-01, 12-08, 12-25, 12-26: .".
I want, for example, to search for elements (ptxxxx) that have a certain name or latitude or category. I have already been advised to use construct in the query to retrieve triples from the rdf, so I can add them to a new graph/rdf that I am building. The fact is that if I filter over triples to search by (case insensitive) name with the regex filter function, I only get one triple that defines in this case the name I was looking for. Is it possible to search for the subject (ptxxxx) whose name is (predicate) "Ufficio Bologna 1" (object), for example?
回答1:
If you want to get the triples for the subject which has that name exactly, you can do a simple describe:
describe ?s where { ?s vcard:fn "Ufficio Bologna 1". }
Otherwise, if you want to use a regex, you can modify the query slightly
describe ?s where { ?s vcard:fn ? name. filter (regex(?name, "regex goes here")). }
Though a word of caution, regex over large datasets can be expensive. Many systems offer extensions to SPARQL which will full-text index your literal values which you can then search over using a more normal search syntax.
回答2:
I solved like this:
CONSTRUCT {?s ?p ?o}
WHERE {
?s ?p ?o;
vcard:fn ?name.
FILTER regex (?name ,"^ufficio bologna 1$", "i")
}
来源:https://stackoverflow.com/questions/10244600/search-by-name-in-rdf-n3-file