Can these types of SPARQL queries run?

Suppose we have a huge RDF graph and we want to do the following: (I tried it and it doesn't work - wanted to know if I am creating the request wrong or some problem with the rdf dump).

select ?n ?o 
where {
    ?n <name_of_a_node> <name_of_this_node>.
    ?n ?p ?o.
    ?o <type_of_a_node> ?t.
    FILTER(REGEX(STR(?t), "president")).
}

      

The above request states that I know the name of node n. So I am getting the URI node n. Then I retrieve all the predicates of node n and the other nodes bind that predicate. For each of these o nodes associated with node a, I want to look at their property (type property) and get only those o nodes that have a substring in their type property.

Is this possible in SPARQL? Basically, stands on a node and looks at all the other nodes that this node is connected to, and then fetches only those nodes that match a different condition of their properties.

Otherwise, should I just get all the o nodes that node n is connected to and, for each of them, run another SPARQL query to do this check?

I am using JENA for data storage.

+3


source to share


1 answer


Yes, it is possible, but writing such a query is almost always very bad.

Anyone FILTER

requires a SPARQL engine to look at all possible solutions and evaluate the expression within it, while many expressions are fast and cheap to evaluate some, such as REGEX

very expensive.



Basically, you are asking the SPARQL engine to get a large, unbounded sequence of possible results, and then apply a regular expression against any possible solution. If you know anything about Java regex performance , then you would know that this is often a very bad idea, regardless of how it is used in SPARQL.

Many SPARQL engines support full-text extensions that allow you to express such queries in a way that the SPARQL engine handles much more efficiently. For Apache Jena see LARQ

+5


source







All Articles