在传递 SPARQL 查询中排序
Ordering in transitive SPARQL query
是否可以保证 SPARQL 中传递查询的结果按照它们被遍历的顺序返回?
所以,给定一些简单的数据:
<http://example.com/step0> ex:contains <http://example.com/step1>
<http://example.com/step1> ex:contains <http://example.com/step2>
<http://example.com/step2> ex:contains <http://example.com/step3>
(实际上这个关系可以重复很多次)
查询(使用 sparql 1.1):
SELECT ?parent
WHERE {
?parent ex:contains* <http://example.com/step3>
}
这样你总能回来 [step0, step1, step2]。在耶拿尝试这个时,我得到了一致但随机排序的结果。
或者,如果我能在传递遍历中同时取回父子关系就好了,这样我就可以在外面重新排序,但我没有知道如何绑定 ?parent ex:contains* <http://example.com/step3>
并取回中间关系的对象,而无需编写非常慢的带过滤的嵌套查询。
根据您的数据示例,尝试:
SELECT ?parent ?child ?subchid
WHERE {
?parent ex:contains <http://example.com/step3> .
?child ex:contains ?parent .
OPTIONAL { ?subchild ex:contains ?child . }
}
如果并非所有 ex:contains
关系都分为三个级别,您可能需要进行 OPTIONAL
模式匹配。
Is it possible to guarantee that the results of a transitive query in
SPARQL come back in the order in which they were walked?
否(SPARQL 1.1 标准未定义顺序)
在这里,固定的物体和数据是线性路径的事实恰好意味着有一个自然的步行顺序。
由于 Apache Jena SPARQL 的执行是确定性的(在这种情况下),它会以某种顺序出现,只是因为内部结果集合保持顺序。并非所有 Jena 版本都这样做 - 它随着时间的推移而改变。
对于其他非线性路径,没有什么是确定的。数据使用哈希映射存储。
对于简单的线性路径,您可以使用跳数作为排序的度量:
PREFIX ex: <http://example.com/>
SELECT ?start
WHERE
{ ?start (ex:contains)+ ?mid .
?mid (ex:contains)* ex:step3
}
GROUP BY ?start
ORDER BY DESC(COUNT(?mid))
输出:
------------
| start |
============
| ex:step0 |
| ex:step1 |
| ex:step2 |
------------
是否可以保证 SPARQL 中传递查询的结果按照它们被遍历的顺序返回?
所以,给定一些简单的数据:
<http://example.com/step0> ex:contains <http://example.com/step1>
<http://example.com/step1> ex:contains <http://example.com/step2>
<http://example.com/step2> ex:contains <http://example.com/step3>
(实际上这个关系可以重复很多次)
查询(使用 sparql 1.1):
SELECT ?parent
WHERE {
?parent ex:contains* <http://example.com/step3>
}
这样你总能回来 [step0, step1, step2]。在耶拿尝试这个时,我得到了一致但随机排序的结果。
或者,如果我能在传递遍历中同时取回父子关系就好了,这样我就可以在外面重新排序,但我没有知道如何绑定 ?parent ex:contains* <http://example.com/step3>
并取回中间关系的对象,而无需编写非常慢的带过滤的嵌套查询。
根据您的数据示例,尝试:
SELECT ?parent ?child ?subchid
WHERE {
?parent ex:contains <http://example.com/step3> .
?child ex:contains ?parent .
OPTIONAL { ?subchild ex:contains ?child . }
}
如果并非所有 ex:contains
关系都分为三个级别,您可能需要进行 OPTIONAL
模式匹配。
Is it possible to guarantee that the results of a transitive query in SPARQL come back in the order in which they were walked?
否(SPARQL 1.1 标准未定义顺序)
在这里,固定的物体和数据是线性路径的事实恰好意味着有一个自然的步行顺序。
由于 Apache Jena SPARQL 的执行是确定性的(在这种情况下),它会以某种顺序出现,只是因为内部结果集合保持顺序。并非所有 Jena 版本都这样做 - 它随着时间的推移而改变。
对于其他非线性路径,没有什么是确定的。数据使用哈希映射存储。
对于简单的线性路径,您可以使用跳数作为排序的度量:
PREFIX ex: <http://example.com/>
SELECT ?start
WHERE
{ ?start (ex:contains)+ ?mid .
?mid (ex:contains)* ex:step3
}
GROUP BY ?start
ORDER BY DESC(COUNT(?mid))
输出:
------------
| start |
============
| ex:step0 |
| ex:step1 |
| ex:step2 |
------------