Show triples containing a text pattern. The bif:search_excerpt is used to format a short excerpt of
the matching literal in search-engine style.
The query requires V6 or higher.
SELECT ?s
?p
( bif:search_excerpt
( bif:vector
( 'web', 'semantic' ) ,
?o
)
)
WHERE
{
?s ?p ?o .
FILTER
(
bif:contains
( ?o, '"semantic web"' )
)
}
LIMIT 10
What sources talk the most about a given subject? Show the top N graphs containing triples with the given text pattern. Sort by descending triple count.
The query requires V6 or higher.
SELECT ?g
COUNT (*)
WHERE
{
GRAPH ?g
{
?s ?p ?o .
FILTER
(
bif:contains
( ?o, 'dakar AND paris' )
)
}
}
GROUP BY ?g
ORDER BY DESC 2
LIMIT 50
What types of objects contain a text pattern? Find matches, get the type. Group by type, order by count.
The query requires V6 or higher.
SELECT ?tp
COUNT (*)
WHERE
{
GRAPH ?g
{
?s ?p ?o .
?s a ?tp
FILTER
(
bif:contains
( ?o, '"Paris Hilton"' )
)
}
}
GROUP BY ?tp
ORDER BY DESC 2
Given a person, find people with the most interests in common with this person. Show the person, the number of shared interests, and the total number of interests.
The query requires V6 or higher.
SELECT ?p
?n
((
SELECT COUNT (*)
WHERE
{
?p foaf:interest ?i .
?ps foaf:interest ?i
}
))
((
SELECT COUNT (*)
WHERE
{
?p foaf:interest ?i
}
))
WHERE
{
?ps foaf:nick "aeon_phoenix"@en .
{
SELECT DISTINCT ?p
?psi
WHERE
{
?p foaf:interest ?i .
?psi foaf:interest ?i
}
} .
FILTER
( ?ps = ?psi )
?p foaf:nick ?n
}
ORDER BY DESC 3
LIMIT 50
What else are people interested in X interested in? What else do Harry Potter fans like?
The query requires V6 or higher.
SELECT ?i2
COUNT (*)
WHERE
{
?p foaf:interest <http://www.livejournal.com/interests.bml?int=harry+potter> .
?p foaf:interest ?i2
}
GROUP BY ?i2
ORDER BY DESC 2
LIMIT 20
Who writes the most about a topic? Show for each author the number of works mentioning the topic and total number of works. For all documents and posts, we have extracted named entities; the entity count shows the entities which occur in the works of each author. There are statistics about named entities occurring together; these are used to display a list of related entities.
The query requires V6 or higher.
SELECT ?auth
?cnt
((
SELECT COUNT (DISTINCT ?xx)
WHERE
{
?xx dc:creator ?auth
}
))
WHERE
{
{
SELECT ?auth
COUNT (DISTINCT ?d) AS ?cnt
WHERE
{
?d dc:creator ?auth .
?d ?p ?o
FILTER
(
bif:contains
( ?o, 'web AND semantic' )
&&
isIRI ( ?auth )
)
}
GROUP BY ?auth
ORDER BY DESC 2
LIMIT 100
}
}
Show the people a person directly or indirectly knows. Sort by distance and count of connections of the known person.
The query requires V6 or higher.
SELECT ?o
?dist
((
SELECT COUNT (*)
WHERE
{
?o foaf:knows ?xx
}
))
WHERE
{
{
SELECT ?s ?o
WHERE
{
?s foaf:knows ?o
}
}
OPTION (
TRANSITIVE ,
T_DISTINCT ,
T_IN ( ?s ) ,
T_OUT ( ?o ) ,
T_MIN ( 1 ) ,
T_MAX ( 4 ) ,
T_STEP ( 'step_no' ) AS ?dist
) .
FILTER
(
?s = <http://myopenlink.net/dataspace/person/kidehen#this>
)
}
ORDER BY ?dist
DESC 3
LIMIT 50
Given two people, find what chain of acquaintances links them together. For each step in the chain, show the person linked to, the graph linking this person to the previous person, the number of the step, and the number of the path. Note that there may be many paths through which the people are linked.
The query requires V6 or higher.
SELECT ?link
?g
?step
?path
WHERE
{
{
SELECT ?s
?o
?g
WHERE
{
GRAPH ?g
{
?s foaf:knows ?o
}
}
}
OPTION
(
TRANSITIVE ,
T_DISTINCT ,
T_IN ( ?s ) ,
T_OUT ( ?o ) ,
T_NO_CYCLES ,
T_SHORTEST_ONLY ,
T_STEP ( ?s ) AS ?link ,
T_STEP ( 'path_id' ) AS ?path ,
T_STEP ( 'step_no' ) AS ?step ,
T_DIRECTION 3
) .
FILTER
(
?s = <http://myopenlink.net/dataspace/person/kidehen#this>
&&
?o = <http://www.advogato.org/person/mparaz/foaf.rdf#me>
)
}
LIMIT 20
Show names of things surrounding a person. These may be interests, classes of things, other people, and
so forth. For each label, show the count of occurrences, largest count first. This uses the b3s:label
superproperty which includes rdfs:label, dc:title, and other qualities which have a general meaning
of "label".
The query requires V6 or higher.
DEFINE input:inference 'b3s'
SELECT ?s
?lbl
COUNT (*)
WHERE
{
?s ?p2 ?o2 .
?o2 <http://b3s-demo.openlinksw.com/label> ?lbl .
?s foaf:nick ?o .
FILTER
(
bif:contains
( ?o, '"aeon_phoenix"' )
)
}
GROUP BY ?s
?lbl
ORDER BY DESC 3
This example shows how to extract a part of a literal as a variable for use in a numeric comparison.
Suppose there are the following triples inserted:
SQL>sparql
insert into graph <http://b3s-demo.openlinksw.com/label> { <:a>
<:p>
"123 abc" };
callret-0
VARCHAR
_________________________________________________________
Insert into <http://b3s-demo.openlinksw.com/label>, 1 triples -- done
1 Rows. -- 30 msec.
SQL>sparql
insert into graph <http://b3s-demo.openlinksw.com/label> { <:a>
<:p>
"234 abc" };
callret-0
VARCHAR
_________________________________________________________
Insert into <http://b3s-demo.openlinksw.com/label>, 1 triples -- done
1 Rows. -- 0 msec.
In order to extract the numeric part, and then do a numeric ( >.<,= ), you can use atoi (), atol or atof in the filter:
select *
from <http://b3s-demo.openlinksw.com/label>
where
{
?s ?p ?o . filter (bif:atoi (?o) > 130)
}