过滤SPARQL结果

时间:2009-08-04 10:07:19

标签: sparql triplestore

我有以下示例三元组

r1 -> property -> resourceA
r1 -> property -> resourceB
r1 -> property -> resourceC
resourceA -> name -> word1
resourceB -> name -> word2
resourceC -> name -> word4

r2 -> property -> resourceD
r2 -> property -> resourceE
r2 -> property -> resourceF
resourceD -> name -> word1
resourceE -> name -> word2
resourceF -> name -> word3

r3 -> property -> resourceG
r3 -> property -> resourceH
r3 -> property -> resourceI
resourceG -> name -> word5
resourceH -> name -> word6
resourceI -> name -> word7

作为参数,我使用word1和word2。我希望得到所有单词。 word1和word2,与word1和word2一起出现。

此示例的结果必须是:

word1
word2
word3
word4

我真的不知道,如何做到这一点:(

1 个答案:

答案 0 :(得分:2)

假设所有name的谓词word都相同,并且name谓词没有其他三元组:

SELECT DISTINCT ?w {
  ?s <name> ?w
}
ORDER BY ?w

编辑问题后编辑:

SELECT DISTINCT ?w {  # select each word only once

  # match three properties under the same resource
  ?r <property> ?p1, ?p2, ?p3.

  # two of the properties must have names "word1" and "word2"
  ?p1 <name> "word1" .
  ?p2 <name> "word2" .

  # third property name may be anything, including "word1" and "word2"
  ?p3 <name> ?w .
}
ORDER BY ?w  # return words in sorted order