Case insensitive match in SPARQL triple - sparql

Is there a way to make the following query case insensitive?
ASK {
VALUES (?r) { (dbr:Game_of_Thrones) }
{ ?r ?p ?o }
UNION
{ ?s ?r ?o }
UNION
{ ?s ?p ?r }
}
It shall return true no matter if I use dbr:Game_of_Thrones or dbr:game_of_thrones.
I tried using LCASE() but it's not allowed inside the triples. Is there another way?
I'm using the DBpedia SPARQL endpoint (https://dbpedia.org/sparql).

IRIs are case-sensitive for good reasons. If you really want to workaround this then you have to use FILTER(lcase(str(?tmp)) = lcase(str(?r))) in each UNION clause:
ASK {
VALUES (?r) { (dbr:Game_of_Thrones) }
{ ?tmp ?p ?o. FILTER(LCASE(STR(tmp)) = LCASE(STR(?r))) }
UNION
{ ?s ?r ?o. FILTER(LCASE(STR(tmp)) = LCASE(STR(?r))) }
UNION
{ ?s ?p ?r. FILTER(LCASE(STR(tmp)) = LCASE(STR(?r))) }
}
Note, this can result in poor performance at it has to scan over all triples and do String comparison.
For fuzzy search, you should really think about a full-text index.
I'd also suggest omitting the second UNION clause as long as you're looking for resources that are probably not schema entities, thus, never occur in predicate position of an RDF triple.

Related

SPARQL returning empty result when passing MIN(?date1) from subquery into outer query, with BIND((YEAR(?minDate) - YEAR(?date2)) AS ?diffDate)

<This question is now resolved, see comment by Valerio Cocchi>
I am trying to pass a variable from a subquery, that takes the minimum date of a set of dates ?date1 belonging to ?p and passes this to the outer query, which then takes another date ?date2 belonging to ?p (there can be at most 1 ?date2 for every ?p) and subtracts ?minDate from ?date2 to get an integer value for the number of years between. I am getting a blank value for this, i.e. ?diffDate returns no value.
I am using Fuseki version 4.3.2. Here is an example of the query:
SELECT ?p ?minDate ?date2 ?diffDate
{
?p a abc:P;
abc:hasAnotherDate ?date2.
BIND((YEAR(?minDate) - YEAR(?date2)) AS ?diffDate)
{
SELECT ?p (MIN(?date1) as ?minDate)
WHERE
{
?p a abc:P;
abc:hasDate ?date1.
} group by ?p
}
}
and an example of the kind of result I am getting:
|-?p----|-----------------?minDate-------------|-----------------?date2------------- |?diffDate|
|<123>|20012-11-22T00:00:00"^^xsd:dateTime|2008-08-18T00:00:00"^^xsd:dateTime| |
I would expect that ?diffDate would give me an integer value. Am I missing something fundamental about how subqueries work in SPARQL?
It seems you have encountered quite an obscure part of the SPARQL spec, namely how BIND works.
Normally SPARQL is evaluated without regard for the position of atoms, i.e.
SELECT *
WHERE {
?a :p1 ?b .
?b :p2 ?c .}
is the same query as:
SELECT *
WHERE {
?b :p2 ?c .
?a :p1 ?b .}
However, BIND is position dependent, so e.g.:
SELECT *
WHERE {
?a :p1 ?b .
BIND(:john AS ?a)}
is not a valid query, whereas:
SELECT *
WHERE {
BIND(:john AS ?a)
?a :p1 ?b .
}
is entirely valid. The same applies to variables used inside of the BIND, which must be declared before the BIND appears.
See here for more.
To go back to your problem, your BIND is using the ?minDate variable before it has been bound, which is why it fails to produce a value for ?diffDate.
This query should do the trick:
SELECT ?p ?minDate ?date2 ?diffDate
{
?p a abc:P;
abc:hasAnotherDate ?date2.
{
SELECT ?p (MIN(?date1) as ?minDate)
WHERE
{
?p a abc:P;
abc:hasDate ?date1.
} group by ?p
}
BIND((YEAR(?minDate) - YEAR(?date2)) AS ?diffDate) #Put the BIND after all the variables it uses are bound.
}
Alternatively, you could evaluate the difference in the SELECT, like so:
SELECT ?p ?minDate ?date2 (YEAR(?minDate) - YEAR(?date2) AS ?diffDate)
{
?p a abc:P;
abc:hasAnotherDate ?date2.
{
SELECT ?p (MIN(?date1) as ?minDate)
WHERE
{
?p a abc:P;
abc:hasDate ?date1.
} group by ?p
}
}

Sparql CONSTRUCT with DISTINCT

PREFIX content: <http://example.com/content#>
construct { ?s content:field ?o}
WHERE { ?s content:field ?o }
90% of all the ?o I get here are the same URI <http://example.com/name>.
I'm trying to find a way to filter out all quads that have the same value for ?o, so in the end I get a list of quads which are unique by its ?o
I tried DISTINCT ?o CONSTRUCT{...} but from what I saw you cant use DISTINCT on a CONSTRUCT.
How would you filter the returned list of quads
I'm trying to find a way to filter out all quads that have the same
value for ?o, so in the end I get a list of quads which are unique by
its ?o
if it does not matter which exact value is bound to ?s, then a sub-select with a group by ?o is the way to go. Use (SAMPLE(?s) as ?subj) e.g. something like:
`
PREFIX content: <http://example.com/content#>
construct { ?s content:field ?o}
WHERE {
{ select ?o (SAMPLE(?subj) as ?s)
{ ?subj content:field ?o }
group by ?o
}
}
`

SPARQL: Querying the Default Graph

With the RDF query language SPARQL, I'm trying to find a way to do a boolean query (or any other query) for anything not in a Named Graph.
ASK { GRAPH null { ?s ?p ?o } }
Can't find really any documentation on searching specifically within an empty Named Graph. Also tried replacing null with <>, empty, and (nothing).
This query will look for triples in the default graph, then remove ones that are also in a named graph:
SELECT ?s ?p ?o {
?s ?p ?o
FILTER NOT EXISTS { GRAPH ?g { ?s ?p ?o } }
}

insert from two named graphs?

I'm looking for an easy way to insert triples from two or more named graphs (but not the entire unnamed default graph) into another named graph. I'm using GraphDB.
I guess this could be done by writing out the same query multiple times in the WHERE section, wrapped in multiple GRAPH specifications, and then unioning them together, but my WHEREs are long and I'd prefer not to write them out multiple times.
Let's say I have loaded some data like this:
INSERT DATA {
GRAPH <http://example.com/ngA> {
<http://example.com/person1> <http://example.com/name> "Arthur" .
}
GRAPH <http://example.com/ngB> {
<http://example.com/person1> <http://example.com/name> "Brian" .
}
GRAPH <http://example.com/ngC> {
<http://example.com/person1> <http://example.com/name> "Charlie" .
}
}
I can copy all of the triples of a certain pattern from the default unnamed graph into a new named graph with something like this:
INSERT {
GRAPH <http://example.com/ngZ> {
?s <http://example.com/moniker> ?o .
}
}
WHERE
{ ?s <http://example.com/name> ?o }
An easy way to SELECT for triples of a given pattern from two or more (but not all) named graphs is
SELECT *
FROM <http://example.com/ngA>
FROM <http://example.com/ngB>
WHERE
{ ?s <http://example.com/name> ?o }
What if I want to copy those triples, from those specified graphs, into another graph?
I'm getting an error from GraphDB 8.3 (and from the sparql.org validator) when I try to
INSERT {
GRAPH <http://example.com/ngZ> {
?s <http://example.com/moniker> ?o .
}
}
WHERE
{ SELECT *
FROM <http://example.com/ngA>
FROM <http://example.com/ngB>
WHERE
{ ?s <http://example.com/name> ?o } }
Try this query:
PREFIX ex: <http://example.com/>
INSERT {
GRAPH ex:ngZ { ?s ex:moniker ?o }
}
WHERE {
GRAPH ?g { ?s ex:name ?o }
FILTER (?g IN ( ex:ngA, ex:ngB ) )
}
And then:
PREFIX ex: <http://example.com/>
SELECT *
FROM NAMED ex:ngZ
WHERE {
GRAPH ?g { ?s ?p ?o }
} LIMIT 100
Is it what you need?
By the way, there exist COPY (use with caution!) and ADD.
SPARQL Update provides USING and USING NAMED analogous to FROM and FROM NAMED in queries:
The USING and USING NAMED clauses affect the RDF Dataset used while evaluating the WHERE clause. This describes a dataset in the same way as FROM and FROM NAMED clauses
You can express the requirement as an UPDATE like so:
INSERT {
GRAPH <http://example.com/ngZ> {
?s <http://example.com/moniker> ?o .
}
}
USING <http://example.com/ngA>
USING <http://example.com/ngB>
WHERE
{ ?s <http://example.com/name> ?o }
Also note that, according to the SPARQL query grammar, a subquery does not admit a dataset clause. This is why the SPARQL parsers are rejecting your query.
Thanks, #Stanislav Kralin
Come to think of it, this also works:
PREFIX ex: <http://example.com/>
INSERT {
GRAPH ex:ngZ {
?s ex:moniker ?o
}
}
WHERE {
values ?g {
ex:ngA ex:ngB
}
GRAPH ?g {
?s ex:name ?o
}
}

Is it possible to Filter Graphs in a way that they at most contain requested Data?

Let me start with an example query to explain my problem:
SELECT ?g ?s ?p ?o WHERE
{
{GRAPH ?g
{ ?s ?p ?o.
OPTIONAL{ ?s
ab:temperature ?temperature.}
FILTER (?temperature = 20)
FILTER NOT EXISTS {?s ab:person ?person}
}
}
}
This query gives me all graphs (in this case representing context data) that have a temperature of 20 but don't have a person associated. My problem is I want to query the graphs for certain optional properties but they shouldn't have any other properties. At the time of the query I only know the OPTIONAL part but I don't know which additional property might be there. Is there an easy way to do this with SPARQL or is that something that would be easier to check after I received the graph and converted it to an object which I can handle with my programm?
If i understand your question correctly, you are searching for graphs that only have that subjects with some properties but not others. In that case i'd run something like this:
SELECT ?g ?s ?p ?o WHERE {
GRAPH ?g {
?s ?p ?o.
FILTER NOT EXISTS {
?s ?bad [] .
FILTER (?bad NOT IN ( ab:temperature, ... ) )
}
}
}