Skip to content

Instantly share code, notes, and snippets.

@michahell
Created September 28, 2015 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michahell/c7c65d376f0041a53069 to your computer and use it in GitHub Desktop.
Save michahell/c7c65d376f0041a53069 to your computer and use it in GitHub Desktop.

Virtuoso and some other SPARQL endpoint software extend the SPARQL 1.1 spec with some custom functions. one of them is free text search, which is INCREDIBLY much more faster than using regular expression text searches on, for example, DBPedia.

Here is an example SPARQL query for DBPedia using regex's :

SELECT DISTINCT ?film ?some_studio ?some_producer
WHERE {
    ?film rdf:type <http://dbpedia.org/ontology/Film> .
    ?film foaf:name ?film_title .
    FILTER regex(str(?film_title), "\\bArgo\\b") .
    ?film dbpprop:studio ?some_studio .
    FILTER regex(str(?some_studio), "\\bSmokehouse Pictures\\b") .
    ?film dbpprop:producer ?some_producer .
    FILTER regex(str(?some_producer), "\\bBen Affleck\\b")
}

And here is one (same result) using Virtuoso's bif:contains :

SELECT DISTINCT ?film ?some_studio ?some_producer
WHERE {
    ?film rdf:type <http://dbpedia.org/ontology/Film> .
    ?film foaf:name ?name .
    ?film dbpprop:name ?name .
    ?film foaf:name ?name .
    ?name bif:contains '"Argo"' .
    ?film dbpprop:studio ?some_studio .
    ?some_studio bif:contains '"Smokehouse Pictures"' .
    ?film dbpprop:producer ?some_producer .
    ?some_producer bif:contains '"Ben Affleck"'
}

Notice the set of quotes around the 'free text': this is for including whitespace. You can test the queries for yourself on DBPedia's public SPARQL endpoint:

dbpedia sparql endpoint

Happy SPARQL'ing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment