具有大量UNION的SPARQL查询的替代方案

时间:2013-02-23 00:06:12

标签: rdf sparql semantic-web virtuoso

我在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会抱怨。

3 个答案:

答案 0 :(得分:3)

(这是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" )  }

答案 1 :(得分:2)

在SPARQL 1.1中,有一个values子句可以帮助解决这个问题。它可以让你写:

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

答案 2 :(得分:1)

价值解决方案更加强大,因为它允许使用UNDEF如下(例如):

put '/articles/:id/publish', to: 'articles#publish', as: :public_article

UNDEF具有通配符函数,返回的三元组集合是单独匹配每个值三元组的并集。但当然对于大型数据集,从性能的角度来看可能会放慢速度

相关问题