我在 Virtuoso 中存储了一些命名图,我想从提供的列表中找到匹配最多术语的图。
我的查询以编程方式构建,如下所示:
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)
每个术语都成为另一个 UNION 子句。
有一个更好的方法吗?查询很快变得又长又难看,当术语太多时,Virtuoso 会提示。
最佳答案
(它是 rdfs:label)
另一种写法是:
{ ?match rdfs:label ?X . FILTER (?x in ("term 1", "term 2", "term 3")) }
或(SPARQL 1.0)
{ ?match rdfs:label ?X . FILTER ( ?x = "term 1" || ?x = "term 2" || ?x = "term 3" ) }
关于sparql - 具有大量 UNION 的 SPARQL 查询的替代方案,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15035337/