问题描述
我正在用Jena执行下面的SPARQL查询,其中uriNode
是表示URI
的String
,但是此查询没有任何结果.我已经检查了数据库,并且此URI
确实存在.有什么问题?
I am executing the SPARQL query below with Jena, where uriNode
is a String
representing a URI
, but this query doesn't give me any results. I've already checked the database and this URI
does exist there. What's the problem?
String queryString2 = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +
"PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?author WHERE { " +
"?document dc:creator ?author." +
"FILTER (?document = \"" + uriNode + "\" ). }";
推荐答案
AndyS的答案是正确的,只要您目前正在检查?document
是否为某些特定的 string .由于字符串(通常是文字)不能成为RDF中三元组的主题,因此您将永远不会以这种方式获得任何解决方案.您实际上需要检查它是否是特定的URI.您可以使用这样的filter
来做到这一点
AndyS's answer is right, insofar as that you're currently checking whether ?document
is some particular string. Since strings (and more generally, literals) can't be the subject of triples in RDF, you'll never get any solutions this way. You actually need to check whether it's a particular URI. You could do that with a filter
like this
FILTER( ?document = <http://example.org/node> )
但这真的很浪费[em],因为这样的查询(基于您的查询)会检索 all 中所有带有dc:creator
的东西,然后将它们中的大部分扔掉
but that's really very wasteful, because a query like the following (based on yours) is retrieving all the things with dc:creator
s and then throwing most of them out.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?author WHERE {
?document dc:creator ?author.
FILTER (?document = <http://example.org/node> ).
}
从字面上看,这就像从图书馆目录中索取所有书籍及其作者的清单,然后逐一浏览并丢弃除一本书外的所有书籍的结果.一个更有效的解决方案是要求您真正关心的事情dc:creator
,因为这只会选择所需的数据,而不必进行任何过滤:
It's literally like asking a library catalog for a list of all the books and their authors, and then going through them one by one and discarding the results for all but one book. A much more efficient solution would be to ask for the dc:creator
of the thing that you actually care about, because this will only select the data that you want, and it won't have to do any filtering:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?author WHERE {
<http://example.org/node> dc:creator ?author.
}
如果需要,您可以执行类似的操作,例如选择具有特定标题的文档;使用这样的查询:
You can do a similar thing if you needed, e.g., to select documents with a specific title; use a query like this:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
SELECT ?document WHERE {
?document dc:title "Reply to Hayne" .
# or, if language tags are in use,
# ?document dc:title "Reply to Hayne"@en
}
这篇关于FILTER(?document ="uriNode")查询不返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!