2013-02-23 78 views
3

我有一些存儲在Virtuoso中的命名圖,我想找到與提供的列表中最多的術語匹配的圖。有很多UNION的SPARQL查詢的替代方案

我的查詢編程結構,看起來像這樣:

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

(它的RDFS:標籤)

的另一種方式把它寫爲:

{ ?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") } 
+0

看起來好多了,謝謝。 – 2013-02-23 15:45:46

+0

另一種方法是使用SPARQL 1.1的較新VALUES。不知道回答時這是否可用,但可能值得更新答案。 – 2013-11-26 17:59:51

2

在SPARQL 1.1,有一個values子句可以幫助這個。它可以讓你寫:

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

值解決方案是更強大,因爲它允許使用民主基金如下(例如):

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

民主基金具有通配符功能和返回的三元組是單獨匹配每個值三元組的聯合。但是,當然對於大型數據集,它可能會從性能的角度來放慢