是否可以在SPARQL中执行任意长度的路径查询.假设我有neo4j商店,它有一个只代表PARENT_OF关系的图表(例如,考虑一个家族树).获取一个人的所有祖先的密码查询看起来像
start n (some node from index query) match n<-[:PARENT_OF*]-k return k
Run Code Online (Sandbox Code Playgroud)
如果将此neo存储表示为基于RDF的三元组存储,那么此查询在SPARQL中的外观如何?这甚至是可能的吗?
如果你有这样的数据:
@prefix : <http://stackoverflow.com/q/22210295/1281433/> .
:a :parentOf :b .
:b :parentOf :c .
:c :parentOf :d .
Run Code Online (Sandbox Code Playgroud)
那么你可以使用SPARQL 1.1的属性路径来使用这样的查询:
prefix : <http://stackoverflow.com/q/22210295/1281433/>
select ?ancestor ?descendent where {
?ancestor :parentOf+ ?descendent
}
Run Code Online (Sandbox Code Playgroud)
得到这样的结果:
-------------------------
| ancestor | descendent |
=========================
| :a | :b |
| :a | :c |
| :a | :d |
| :b | :c |
| :b | :d |
| :c | :d |
-------------------------
Run Code Online (Sandbox Code Playgroud)
请注意,使用*允许零关系出现,并将每个节点与自身相关联.如果你希望每件事物都是它自己的祖先,那么你可以在我的查询中替换+它*.