Gravsearch limit to one named graph

Hello @benjamingeer and @t.schweizer,

what shall I add to the default query to limit the search to one graph, let’s say incunabula data graph?

default query:

PREFIX knora-api: <http://api.knora.org/ontology/knora-api/simple/v2#>
PREFIX incunabula: <http://api.dasch.swiss/ontology/0803/incunabula/simple/v2#>

CONSTRUCT {
    ?book knora-api:isMainResource true .
    ?book incunabula:title ?title .

} 

WHERE {
    ?book a incunabula:book .
    ?book incunabula:title ?title .
}

Thanks in advance!
Loïc.

I think Gravsearch would have to support the property attachedToProject to do that. Then you could restrict your search to resources belonging to a specific project.

Hello @t.schweizer thanks for your answer :wink:

(edited)
limiting to one graph could be an easy optimization (we see some slow answers).
attachedToProject still scans all of the graphs, I wonder how scalable it is with a growing number of projects.

If a generated SPARQL query searched only in the incunabula graph, the query wouldn’t work, because it wouldn’t have access to the graphs containing ontologies. But perhaps we could take a data graph IRI as a URL parameter, and use it to generate SPARQL FROM statements including the requested data graph as well as the other necessary graphs.

Or we could accept one SPARQL FROM statement in the query, and expand it to several statements.

limiting to a project with a bind on attachedToProject gives no performance improvement at all.

@benjamingeer : trying to figure out if a from would make sense, I take an existing sparql request resulting from a gravsearch, but I don’t manage to add the limit on a given graph, could you tell me how to do that?

Right now my school case is:

SELECT 
DISTINCT ?s
WHERE { 

    ?s ?p ?o .
    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.knora.org/ontology/knora-base#Resource> .
    
    
    GRAPH <http://www.ontotext.com/explicit> {
      ?o <http://www.ontotext.com/owlim/lucene#fullTextSearchIndex> "Sacc*"^^<http://www.w3.org/2001/XMLSchema#string> .
    }
}
LIMIT 100 

Did you maybe try:

?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.knora.org/ontology/knora-base#Resource> .
?s ?p ?o .

I would also put “s? p? o?” at the end.

thanks for your answer @subotic

s? is a magic that I don’t know or you meant ?s?

regarding the relative position of ?s ?p ?o . and ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.knora.org/ontology/knora-base#Resource> .: it makes no difference, we get the same steps in the same order and the same order of complexity for both:

PREFIX onto: <http://www.ontotext.com/>

SELECT 
DISTINCT ?s
FROM onto:explain {

    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.knora.org/ontology/knora-base#Resource> .
    ?s ?p ?o .
    
    GRAPH <http://www.ontotext.com/explicit> {
      ?o <http://www.ontotext.com/owlim/lucene#fullTextSearchIndex> "Sacc*"^^<http://www.w3.org/2001/XMLSchema#string> .
    }
}
PREFIX onto: <http://www.ontotext.com/>

SELECT 
DISTINCT ?s
FROM onto:explain {

    ?s ?p ?o .
    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.knora.org/ontology/knora-base#Resource> .
    
    GRAPH <http://www.ontotext.com/explicit> {
      ?o <http://www.ontotext.com/owlim/lucene#fullTextSearchIndex> "Sacc*"^^<http://www.w3.org/2001/XMLSchema#string> .
    }
}

but surely, internally it does:

    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.knora.org/ontology/knora-base#Resource> .
    ?s ?p ?o .

Yes, sorry. I meant ?s.

Hello @benjamingeer, I see you committing some code and therefore I come back bugging you: how the hell can I limit search on a given graph, even on such a simple sparql query like the one below?

SELECT 
DISTINCT ?s
WHERE { 

    ?s ?p ?o .
    ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.knora.org/ontology/knora-base#Resource> .
    
    
    GRAPH <http://www.ontotext.com/explicit> {
      ?o <http://www.ontotext.com/owlim/lucene#fullTextSearchIndex> "Sacc*"^^<http://www.w3.org/2001/XMLSchema#string> .
    }
}
LIMIT 100 

Hi @loic.jaouen,

The answer depends on how the triplestore does inference. In your query, ?s a knora-base:Resource can only match an inferred triple. In some triplestores, inference is done on the fly, so the query would need access to at least all the named graphs containing the Knora built-in ontologies, otherwise it not be able to determine whether a given resource is a knora-base:Resource.

This seems to be the relevant GraphDB documentation:

http://graphdb.ontotext.com/documentation/standard/query-behaviour.html

In GraphDB, inferred triples go into the triplestore’s default graph.

If I make a query like this, it finds nothing:

PREFIX knora-base: <http://www.knora.org/ontology/knora-base#>

SELECT 
DISTINCT ?s
FROM <http://www.knora.org/data/0803/incunabula>
WHERE {
    ?s a knora-base:Resource .        
    ?s ?p ?o .
    ?o <http://www.ontotext.com/owlim/lucene#fullTextSearchIndex> "Zeitglöcklein" .
}
LIMIT 10

This is because the FROM excludes everything but the incunabula graph, which doesn’t contain the inferred triples.

I can make the query work by including the inferred triples like this:

PREFIX knora-base: <http://www.knora.org/ontology/knora-base#>

SELECT 
DISTINCT ?s
FROM <http://www.knora.org/data/0803/incunabula>
FROM NAMED <http://www.ontotext.com/implicit>
WHERE { 
    GRAPH <http://www.ontotext.com/implicit> {
        ?s a knora-base:Resource .        
    }
    
    ?s ?p ?o .
    ?o <http://www.ontotext.com/owlim/lucene#fullTextSearchIndex> "Zeitglöcklein" .
}
LIMIT 10

If I search in the BEOL named graph instead, I get no results, as expected:

PREFIX knora-base: <http://www.knora.org/ontology/knora-base#>

SELECT 
DISTINCT ?s
FROM <http://www.knora.org/data/0801/beol>
FROM NAMED <http://www.ontotext.com/implicit>
WHERE { 
    GRAPH <http://www.ontotext.com/implicit> {
        ?s a knora-base:Resource .        
    }
    
    ?s ?p ?o .
    ?o <http://www.ontotext.com/owlim/lucene#fullTextSearchIndex> "Zeitglöcklein" .
}
LIMIT 10

amazing @benjamingeer
many thanks Ben for that great answer :slight_smile:

1 Like