How to filter the simple Subject in a SPARQL Query - sparql

I guess I am stuck at the basics with SPARQL. Can someone help ?
I simply wnat to filter all subjects containing "Mountain" of an RDS database.
Prefix lgdr:<http://linkedgeodata.org/triplify/> Prefix lgdo:<http://linkedgeodata.org/ontology/>
Select * where {
?s ?p ?o .
filter (contains(?s, "Mountain"))
} Limit 1000
The query leads to an error:
Virtuoso 22023 Error SL001: The SPARQL 1.1 function CONTAINS() needs a string value as first argument

You can get it to "work" using:
Prefix lgdr:<http://linkedgeodata.org/triplify/> Prefix lgdo:<http://linkedgeodata.org/ontology/>
Select * where {
?s ?p ?o .
filter (contains(str(?s), "Mountain"))
} Limit 1000
Note the additional str in the query.
However, that results in
Virtuoso S1T00 Error SR171: Transaction timed out
and I am not sure how to deal with that.
But in principle in works: When you use
Limit 1
you get
s p o
http://linkedgeodata.org/ontology/MountainRescue http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2002/07/owl#Class

Related

Meaning of the very simple SPARQL query on LinkedGeoData endpoint?

I am a newbie to SPARQL. Though I have read some materials about RDF and SPARQL, I still cannot understand the meaning of the mysterious SPARQL query on the LinkedGeoData SPARQL endpoint
Prefix lgdr:<http://linkedgeodata.org/triplify/>
Prefix lgdo:<http://linkedgeodata.org/ontology/>
Select * { ?s ?p ?o . }
Limit 1000
What does this oversimplified where condition ?s ?p ?o mean?
The query you ask about will return 1,000 triples from the endpoint with no filter or condition applied i.e. {?s ?p ?o. } will match any triple.
It's similar to SELECT * FROM aView in SQL if aView was a union of all, or most, of the tables in a SQL database.

"Sparql filter by number of objects return for a given predicate

In the following query I'm trying to get a list of all entries ?s that include more than 3 objects for the predicate sctap:mentionedBy. However, I keep getting a malformed query error for this search. Does anyone see anything wrong with my query?
Thanks
SELECT ?s
WHERE {
?s sctap:mentionedBy ?o
FILTER (count(?o) > 3)
}
The sparql error says: "Aggregate expression not legal". I'm not sure what that means.
Does anyone see anything wrong with my query?
Sure. Just like the error message says, you're using an aggregate expression (count(?o)) where one isn't legal. You can see in the table of contents of SPARQL 1.1 Query Language what things are filter functions that you can use in a filter, and what things are aggregates, and where you can use each. You can also try parsing queries at sparql.org's query validator. For your query, it will give you the line and column numbers where something went wrong. It's at count(?o).
In this case, you're trying to count the number of ?o values for each s, which means that you need to group by ?s, and that your filter will need to be father out. E.g.,
select ?s where {
?s sctap:mentionedBy ?o
}
group by ?s
having (count(?o) > 3)
It's unlikely to make a difference in this case, but you probably only want to count distinct values of ?o, so you could also consider:
select ?s where {
?s sctap:mentionedBy ?o
}
group by ?s
having (count(distinct ?o) > 3)

Japanese Virtuoso SPARQL EndPoint times out very quickly

I am trying to use the following query on Japanese dbpedia SPARQL Endpoint
select ?s (group_concat(?album_ja ; separator = "|") AS ?name_album_ja_csv) where{
values ?sType { dbpedia-owl:Song
dbpedia-owl:Single
} .
?s a ?sType .
?s (dbpedia-owl:album|^dbpedia-owl:album)* ?albums;rdfs:label ?album_ja
}group by ?s order by ?s offset 0 limit 10
but I get this error Virtuoso 42000 Error The estimated execution time 1005 (sec) exceeds the limit of 400 (sec). Almost any query involving group by has this problem. Is this a server problem? Is my query inefficient? How can I get around it?
I'm not sure exactly what you're trying to do, but since ?o isn't used in the results, you can get rid of it. Even after you do that, though, you'll still have the same problem. You need to change the property path somehow. I don't think that you actually need arbitrary length paths in both directions. You could use ? instead of * to say "path of length 0 or 1" and thus:
select ?s
(group_concat(?album_ja ; separator = "|") AS ?name_album_ja_csv)
where {
values ?sType { dbpedia-owl:Song dbpedia-owl:Single }
?s a ?sType .
?s (dbpedia-owl:album|^dbpedia-owl:album)? ?album_ja
}
group by ?s
limit 100
SPARQL results
Note that a path of length 0 is going to be a link to itself, so one value of ?album_ja is always going to be the same as ?s. Is this really what you want?

SPARQL queries for Pizza ontology

i have to use ROWLKit
http://www.dis.uniroma1.it/quonto/?q=node/30
(1) can anybody suggest two sparql queries for the Pizza.owl ?
(2) is this query valid ?
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT *
WHERE { ?p rdf:type pizza:Pizza;
pizza:hasTopping ?t.
?t rdf:type pizza:TomatoTopping }
(3) if it is a valid query then: is the response an empty result?
SELECT DISTINCT *
WHERE {
?NombrePizza ?Relacion pizza:MushroomTopping .
?Relacion owl:inverseOf pizza:isToppingOf .
OPTIONAL {
?NombrePizza2 ?Relacion2 pizza:HamTopping .
?Relacion2 owl:inverseOf pizza:isToppingOf .
}
FILTER(?NombrePizza2 = ?NombrePizza)
}
(1) can anybody suggest two sparql queries for the Pizza.owl ?
Here are two examples:
SELECT * WHERE { ?s ?p ?o }
and:
SELECT ?class WHERE { ?class a owl:Class }
(2) is this query valid ?
Yes.
(3) if it is a valid query then: is the response an empty result?
I assume that you mean "if I query the RDF document that serialises the pizza ontology, is the response an empty result?". The answer is yes.
(2) appears to be a valid query
I don't understand part (3) of your question. (2) cannot be compared to a boolean since it returns a Result Set, if you want a boolean result then you need to use an ASK query. If an ASK query returns true then it means that there are solutions to the query in the data you are querying so it would not be an empty result.

SPARQL Query problem -> wrong answer

I want to select a triple using SPARQL. To do it, i'm using following query:
SELECT count (*)
WHERE {?s ?p ?o}
FILTER (?s=http://kjkhlsa.net && ?p=http://lkasdjlkjas.com && ?o=Test)
As answer i get fully wrong triple :( subject ist not equal to "http://kjkhlsa.net", predicate is not equal to "http://lkasdjlkjas.com" and object ist also not equal to "Test". Can someone explain me, what I'm doing wrong :(
edit1:
I have put the query into php file:
$inst_query = 'SELECT * { <http://kjkhlsa.net> <http://lkasdjlkjas.com> "Test"}';
echo $inst_query;
The answer from the echo was "SELECT * { "Test"}". Then i tried it with WHERE:
$inst_query = 'SELECT * WHERE { <http://kjkhlsa.net> <http://lkasdjlkjas.com> "Test"}';
echo $inst_query;
Here was the answer "SELECT * WHERE { "Test"}"...so, i'm missing the URIs, but this seems for me as php issue and not sparql problem.
edit2:
I've put the query into SPARQL Query editor and i get the response "no result"....but I'm sure, that i have this triple.
In its current form the question is not very clear (see my comment above).
Since you are essentially trying to get triples matching a pattern, it is more efficient to use a graph pattern instead of FILTER. Many SPARQL implementations first match candidate triples by graph patterns and only then apply the FILTER expression. In essence, with a ?s ?p ?o graph pattern, you're doing a linear scan over all your triples.
So, here's something that should work, using graph patterns instead of FILTER.
SELECT * { <http://kjkhlsa.net> <http://lkasdjlkjas.com> "Test" }
Notes: I didn't include COUNT(*) which is not standard SPARQL. <> around URIs. "" around literal.
Try to use this :
SELECT count (*) as ?count
WHERE {
?s ?p ?o
FILTER (?s=<http://kjkhlsa.net> && ?p=<http://lkasdjlkjas.com> && ?o=Test)
}
The following query uses the count function to count the number of distinct URI(s) returned to the ?s variable.
SELECT ?s (COUNT (DISTINCT ?s) as ?count)
WHERE {?s ?p ?o}
FILTER (?o="Test")