Excluding triples based on substring - sparql

I have a graph created by sponging web pages. I'm trying to write a query to exclude all triples where the subject is a web page. I can get these triples simply by
FILTER (STRENDS((STR(?i)), "html"))
but what's the best way to exclude them. Basically I need all {?s ?p ?o} minus the subset {?i ?p ?o}.

This
SELECT *
{ ?s ?p ?o
FILTER ( ! STRENDS( STR(?s), "html") )
}
filters the outcome the pattern.

Related

SPARQL Query to second and third level with concat

[I have a Tree Structure with three levels currently I am able to get the first level data from the the query written below I am looking for a query that can help me to concat data for ?o and their respective ?s ?p and ?o and even drill down to third level if needed
I have tried it with construct query but want it in a specific format do trying it out with select and concat
construct{ ?CR ?p ?cOBJECT .}
where {
select ?CR ?p (group_concat(?o;separator=',') as ?cOBJECT)
where{ VALUES ?p {abc:hasPart abc:hasLog abc:Description abc:CRStatus abc:hasSB abc:hasTB}
?CR ?p ?o .
}
GROUP BY ?CR ?p
}

SPARQL: How to get all triples with certain predicate where only one direction exists

I want to query all triples with a certain predicate p. The query should only return triples (s,p,o) where the other direction (o,p,s) does not exist.
How can I make such a query?
That's pretty easy:
SELECT ?s ?p ?o {
?s ?p ?o
MINUS { ?o ?p ?s }
}
FILTER NOT EXISTS instead of MINUS would also work. Replace ?p in the query with the desired predicate, or use something like FILTER (?p=ex:myPredicate) if you want ?p in the result.

sparql how to count variable pairs

I have the following query that gets instances of a class and their label/names. I want to count how many total results there are. However, I do not know how to formulate the count statement.
select ?s ?l {
?s a <http://dbpedia.org/ontology/Ship> .
{?s <http://www.w3.org/2000/01/rdf-schema#label> ?l}
union
{?s <http://xmlns.com/foaf/0.1/name> ?l}
}
I have tried
select ?s ?l (count (?s) as ?count) {
?s a <http://dbpedia.org/ontology/Ship> .
{?s <http://www.w3.org/2000/01/rdf-schema#label> ?l}
union
{?s <http://xmlns.com/foaf/0.1/name> ?l}
}
But that gives the counting for each ?s ?l pair, instead I need to know how many of the ?s ?l pairs there are. Or maybe I should not use count at all? As mentioned all I need to know is how many results in total a query returns (regardless of the hard limit that is put by the server, e.g., DBPedia returns a maximum of 50000 results for each query).
Any suggestions please?
Many thanks!
To count the number of matches, use
SELECT (COUNT(*) AS ?count)
WHERE {
?s <http://www.w3.org/2000/01/rdf-schema#label> | <http://xmlns.com/foaf/0.1/name> ?l .
}
Note I'm using the property path "or" (|) to get the union of the properties.

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.

Behaviour of variables projected out of the sub-select

Given a list of entities (with Persons among them) and their properties how should the following query behave:
select *
where
{
?s ?p ?o.
{
SELECT ?ps WHERE
{
?ps a <http://www.example.org/schema/Person> .
}
limit 1
}
#?ps ?p ?o.
filter (?s =?ps)
}
I tested this in 3 triple stores. Two of them filter on ps with the above query so the result is triples for one person(+ps column).
The 3'rd one returns all database triples because "The variable "ps" that is projected out of the sub-select does not join with anything in the top-level query."
Still since it's projected out and I use it in a FILTER I would expect to apply the filter.
Uncommenting line " #?ps ?p ?o. " will indeed display triples for one person.
The filter will be applied.
The FILTER applies to the whole block. There is a join of results of "?s ?p ?o" with results ?ps (so it's a join that is a cross product at this point - no common variable - but that's OK). That results in solutions with 4 bindings ?s ?p ?o ?ps The filter then applies.
You could write:
WHERE {
?s ?p ?o.
{
SELECT ?s
WHERE { ?s a <http://www.example.org/schema/Person> . }
limit 1
}
}