Blazegraph full text search with total count - sparql

In SPARQL I can perform the following query in order to retrieve the fields and the total count in one query result:
SELECT ?total ?s
WHERE
{
{ SELECT (COUNT(?s) AS ?total)
WHERE
{ ?s <https://some/predicate/for/var1> ?var1 ;
<https://some/predicate/for/var1> ?var2
FILTER ( ( ?var1 = "something" ) && ( ?var2 = "something2" ) )
}
}
{ SELECT ?s
WHERE
{ ?s <https://some/predicate/for/var1> ?var1 ;
<https://some/predicate/for/var1> ?var2
FILTER ( ( ?var1 = "something" ) && ( ?var2 = "something2" ) )
}
LIMIT 3
}
}
Which returns something like this (s fields matched the specific predicate and filter I provided on the query):
+-------+----------------------------------------+
| total | s |
+-------+----------------------------------------+
| 150 | http://the/path/to/the/subject |
| 150 | http://the/path/to/another/subject |
| 150 | http://the/path/to/yet/another/subject |
+-------+----------------------------------------+
I want to do the same for full text search queries, which can be used as follows ?s bds:search “something” .
However, composing a query with the same structure as the previous one, does not work:
PREFIX bds: <http://www.bigdata.com/rdf/search#>
SELECT ?total ?s ?org
WHERE
{
{ SELECT (COUNT(?s) AS ?total)
WHERE{
?matchedValue
bds:search "something" ;
bds:relevance ?score ;
bds:rank ?rank .
?s ?matchedProperty ?matchedValue
FILTER ( ! isBlank(?s) )
}
}
{ SELECT ?s ?matchedProperty ?score ?rank
WHERE{
?matchedValue
bds:search "something" ;
bds:relevance ?score ;
bds:rank ?rank .
?s ?matchedProperty ?matchedValue
FILTER ( ! isBlank(?s) )
}
LIMIT 10
}
}
Even though, those subqueries return the correct result separately.

As correctly mentioned by #stanislav-kralin and coming from the issue in Jira, you have to explicitly use SERVICE clause:
PREFIX bds: <http://www.bigdata.com/rdf/search#>
SELECT ?total ?s ?org
WHERE
{
{ SELECT (COUNT(?s) AS ?total)
WHERE {
SERVICE <http://www.bigdata.com/rdf/search#search> {
?matchedValue
bds:search "something" ;
bds:relevance ?score ;
bds:rank ?rank .
}
?s ?matchedProperty ?matchedValue
FILTER ( ! isBlank(?s) )
}
}
{ SELECT ?s ?matchedProperty ?score ?rank
WHERE {
SERVICE <http://www.bigdata.com/rdf/search#search> {
?matchedValue
bds:search "something" ;
bds:relevance ?score ;
bds:rank ?rank .
}
?s ?matchedProperty ?matchedValue
FILTER ( ! isBlank(?s) )
}
LIMIT 10
}
}

Related

Unable to see Max in SPARQL Query

Im trying to query a knowledge graph and im trying print the max occurrence of ?n in the result and i have tried running following query but it just doesn't prints anything
here is my SPARQL Query
PREFIX : <http://www.tafsirtabari.com/ontology#>
PREFIX RDF:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
select
?n
(MAX( xsd:int(?countOfSharedLikedItems)) as ?max)
(COUNT(?n) as ?countOfSharedLikedItems)
where {
?h :hasTheme :lugha .
?h RDF:type :Hadith .
?h :hasHadithNo ?o.
?p :isPartOfHadith ?h.
{
?p :hasNarratorSegment ?nc.
?nc :refersTo+/:hasName ?n.
}
Union
{
?p :hasRootNarratorSegment ?rnc.
?rnc :refersTo+/:hasName ?n.
}
}
i have also tried following by using group by ?n
PREFIX : <http://www.tafsirtabari.com/ontology#>
PREFIX RDF:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select
(MAX(?countOfSharedLikedItems) as ?max)
(COUNT(?n) as ?countOfSharedLikedItems)
where {
?h :hasTheme :lugha .
?h RDF:type :Hadith .
?h :hasHadithNo ?o.
?p :isPartOfHadith ?h.
{
?p :hasNarratorSegment ?nc.
?nc :refersTo+/:hasName ?n.
}
Union
{
?p :hasRootNarratorSegment ?rnc.
?rnc :refersTo+/:hasName ?n.
}
} group by ?n
You can try this
PREFIX : <http://www.tafsirtabari.com/ontology#>
select (COUNT(?o ) AS ?triples) where {
?k :heardFrom ?o
}
6. Which RAWI narrated most hadiths about TOPIC_A
PREFIX hash: <http://www.tafsirtabari.com/ontology#>
PREFIX W3:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX : <http://www.tafsirtabari.com/ontology#>
SELECT ?total WHERE{
select DISTINCT ?n (COUNT(?n) as ?total) where {
?commentary hash:mentions hash:اهل_المعرفه .
?segment hash:containsCommentary ?commentary.
?segment ?Fr ?h .
?h W3:type hash:Hadith.
?p :isPartOfHadith ?h.
{
?p :hasNarratorSegment ?nc.
?nc :refersTo+/:hasName ?n.
}
Union
{
?p :hasRootNarratorSegment ?rnc.
?rnc :refersTo+/:hasName ?n.
}
}GROUP BY ?n
}ORDER BY DESC(?total)
LIMIT 1

Is there a WITH clause in SPARQL?

Is there any analog of view creation or WITH clause from SQL in SPARQL?
I want to:
Select some data from table;
Select data which is not in the first selection (by subject) from the same table.
This seems to work without "WITH" but i do two similar SELECT query:
SELECT *
FROM NAMED <http://...>
{
{
SELECT DISTINCT ?s ?p ?o
WHERE {
BIND (<http://www.w3.org/2004/02/skos/core#prefLabel> AS ?p)
{ ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2004/02/skos/core#Concept> . }
FILTER(LANGMATCHES(LANG(?o), "ru"))
}
ORDER BY ?o
}
UNION
{
SELECT DISTINCT ?s ?p ?o
WHERE {
BIND (<http://www.w3.org/2004/02/skos/core#prefLabel> AS ?p)
{ ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2004/02/skos/core#Concept> . }
FILTER(LANGMATCHES(LANG(?o), "en"))
MINUS {
SELECT DISTINCT ?s
WHERE {
BIND (<http://www.w3.org/2004/02/skos/core#prefLabel> AS ?p)
{ ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2004/02/skos/core#Concept> . }
FILTER(LANGMATCHES(LANG(?o), "ru"))
}
}
}
ORDER BY ?o
}
}

Using SPARQL "from" with both a graph and a graph group in Virtuoso 7.20.3217

I have a graph group <group> of m graphs <group_1>...<group_m> with n total triples. When I do a count together with a graph <graph> with k total triples outside of the graph group, I only get the number of triples n in the graph group:
select count(*)
from <group>
from <graph>
{?s ?p ?o}
Result: n
When I list the graphs in the graph group explicitly, however, I get the correct result:
select count(*)
from <group_1>
from <group_2>
...
from <group_m>
from <graph>
{?s ?p ?o}
Result: n + k
How can I obtain the correct result with the graph group and what is the reason for this behaviour?
You should use either two subqueries like this for example:
select ?n ?k (?n + ?k as ?totalCount) where {
{ select (count(*) as ?n) where {
graph group: { ?s ?p ?o } }
{ select (count(*) as ?k) where {
graph graph: { ?s ?p ?o } }
}
Or use a union:
select (count(?s1) as ?n)
(count(?s2) as ?k)
(?n + ?k as ?totalCount)
where {
{ graph group: { ?s1 ?p1 ?o1 } }
union
{ graph graph: { ?s2 ?p2 ?o2 } }
}

Get URI in results of dbpedia SPARQL query

I have a list of dbpedia URI's and I want to get some informations (categories, label) about each of them in one query:
SELECT ?category ?label where {
{
dbpedia:Financial_Times dcterms:subject ?category .
dbpedia:Financial_Times rdfs:label ?label .
FILTER ( lang(?label) = 'en' )
}
UNION
{
dbpedia:London dcterms:subject ?category .
dbpedia:London rdfs:label ?label .
FILTER ( lang(?label) = 'en' )
}
}
This query works fine, but I'd need to add the URI's themself into the result to be able identify which result row is for which URI.
you can do something like
SELECT distinct ?who ?category ?label where {
{
?who dcterms:subject ?category .
?who rdfs:label ?label .
FILTER ( lang(?label) = 'en' ).
FILTER(?who = dbpedia:Financial_Times or ?who = dbpedia:London )
}}
or use a trick like this
SELECT ?who ?category ?label where {
{
dbpedia:Financial_Times dcterms:subject ?category .
dbpedia:Financial_Times rdfs:label ?label .
FILTER ( lang(?label) = 'en' ).
VALUES ?who { dbpedia:Financial_Times}
}
UNION
{
dbpedia:London dcterms:subject ?category .
dbpedia:London rdfs:label ?label .
FILTER ( lang(?label) = 'en' ) .
VALUES ?who { dbpedia:London }
}}
the second one probably is faster but needs SPARQL 1.1

UNION operator in SPARQL updates

I have two SPARQL updates.First one:
INSERT
{ GRAPH <[http://example/bookStore2]> { ?book ?p ?v } }
WHERE
{ GRAPH <[http://example/bookStore]>
{ ?book dc:date ?date .
FILTER ( ?date > "1970-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
} }
Second:
INSERT
{ GRAPH <[http://example/bookStore2]> { ?book ?p ?v } }
WHERE
{ GRAPH <[http://example/bookStore3]>
{ ?book dc:date ?date .
FILTER ( ?date > "1980-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
} }
Can i combine them with the UNION operator? And if yes, is it an equivalent result? Is it possible to use UNION in SPARQL updates such as in "Select"?
AndyS's answer is correct; you can combine them, and the description of UNION is found in section 7 Matching Alternatives of the SPARQL specification. The combined query would be:
INSERT {
GRAPH <[http://example/bookStore2]> { ?book ?p ?v }
}
WHERE{
{
GRAPH <[http://example/bookStore]> {
?book dc:date ?date .
FILTER ( ?date > "1970-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
}
}
UNION
{
GRAPH <[http://example/bookStore3]> {
?book dc:date ?date .
FILTER ( ?date > "1980-01-01T00:00:00-02:00"^^xsd:dateTime )
?book ?p ?v
}
}
}
In this particular case where the patterns are so similar, you could also just abstract out the differing parts with VALUES:
INSERT {
GRAPH <[http://example/bookStore2]> { ?book ?p ?v }
}
WHERE{
values (?graph ?startDate) {
(<[http://example/bookStore]> "1970-01-01T00:00:00-02:00"^^xsd:dateTime)
(<[http://example/bookStore3]> "1980-01-01T00:00:00-02:00"^^xsd:dateTime)
}
GRAPH ?graph {
?book dc:date ?date .
FILTER ( ?date > ?startDate )
?book ?p ?v
}
}
The WHERE clause is the same as SPARQL Query - you can use UNION.