Alternatives to SPARQL query with lots of UNIONs

让人想犯罪 __ 提交于 2019-12-12 14:23:12

问题


I have some named graphs stored in Virtuoso, and I want to find the one that matches the highest number of terms from a provided list.

My query is constructed programatically and looks like this:

SELECT DISTINCT ?graph (count(DISTINCT ?match) as ?matches)
WHERE {
  GRAPH ?graph {
    {?match rdf:label "term 1"} 
     UNION {?match rdf:label "term 2"} 
     UNION {?match rdf:label "term 3"}
     ...
  }
}
ORDER BY DESC(?matches)

Each term becomes another UNION clause.

Is there a better way to do this? The query gets long and ugly fast, and Virtuoso complains when there are too many terms.


回答1:


(it's rdfs:label)

An alternative way to write it is:

{ ?match rdfs:label ?X . FILTER (?x in ("term 1", "term 2", "term 3")) }

or (SPARQL 1.0)

{ ?match rdfs:label ?X . FILTER ( ?x = "term 1" || ?x = "term 2" || ?x = "term 3" )  }



回答2:


In SPARQL 1.1, there's a values clause that can help out with this. It lets you write:

select ?match where {
  values ?label { "term 1" "term 2" "term 3" }
  ?match rdfs:label ?label
}



回答3:


The values solution is even more powerful as it allows the use of UNDEF as follows (e.g.):

VALUES (?s ?p ?o) { (<http://abc#X> <http://abc#P1> UNDEF)
                    (UNDEF <http://abc#P2> <http://abc#Y>) }

UNDEF has a wildcard function and the returned set of triplets is the union of matching each value triplet individually. But of course for large datasets it might be to slow from a performance point of view



来源:https://stackoverflow.com/questions/15035337/alternatives-to-sparql-query-with-lots-of-unions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!