我正在开发一个java应用程序,它使用ARQ在TDB上使用Fuseki端点执行SPARQL查询。
应用程序需要一个查询来返回每个人的出生地以及在同一地点出生的其他人的出生地。
首先,我编写了这个SPARQL查询,它返回person_ids和每个人的出生地。
prefix fb: <http://rdf.freebase.com/ns/>
prefix fn: <http://www.w3.org/2005/xpath-functions#>
select ?person_id ?place_of_birth
where {
?person_id fb:type.object.type fb:people.person .
?person_id fb:people.person.place_of_birth ?place_of_birth_id .
?place_of_birth_id fb:type.object.name ?place_of_birth .
FILTER (langMatches(lang(?place_of_birth),"en"))
}
LIMIT 10
----------------------------------
| person_id | place_of_birth |
==================================
| fb:m.01vtj38 | "El Centro"@en |
| fb:m.01vsy7t | "Brixton"@en |
| fb:m.09prqv | "Pittsburgh"@en |
----------------------------------在那之后,我添加了一个子查询(https://jena.apache.org/documentation/query/sub-select.html),添加了在那里出生的其他人,但我得到了多个相关的人,并且我只需要一个。
prefix fb: <http://rdf.freebase.com/ns/>
prefix fn: <http://www.w3.org/2005/xpath-functions#>
select ?person_id ?place_of_birth ?other_person_id
where {
?person_id fb:type.object.type fb:people.person .
?person_id fb:people.person.place_of_birth ?place_of_birth_id .
?place_of_birth_id fb:type.object.name ?place_of_birth .
{
select ?other_person_id
where {
?place_of_birth_id fb:location.location.people_born_here ?other_person_id .
}
}
FILTER (langMatches(lang(?place_of_birth),"en"))
}
LIMIT 10
---------------------------------------------------
| person_id | place_of_birth | other_person_id |
===================================================
| fb:m.01vtj38 | "El Centro"@en | fb:m.01vtj38 |
| fb:m.01vtj38 | "El Centro"@en | fb:m.01vsy7t |
| fb:m.01vtj38 | "El Centro"@en | fb:m.09prqv |
---------------------------------------------------我试图添加一个限制为1的子查询,但似乎不起作用(该查询已执行,但从未结束)
prefix fb: <http://rdf.freebase.com/ns/>
prefix fn: <http://www.w3.org/2005/xpath-functions#>
select ?person_id ?place_of_birth ?other_person_id
where {
?person_id fb:type.object.type fb:people.person .
?person_id fb:people.person.place_of_birth ?place_of_birth_id .
?place_of_birth_id fb:type.object.name ?place_of_birth .
{
select ?other_person_id
where {
?place_of_birth_id fb:location.location.people_born_here ?other_person_id .
}
LIMIT 1
}
FILTER (langMatches(lang(?place_of_birth),"en"))
}
LIMIT 3有没有办法在子查询中只返回一个结果,或者我不能使用SPARQL来做到这一点。
发布于 2014-11-21 21:42:27
您可以对子查询使用限制
您可以在子查询中使用限制。下面是一个例子:
select ?x ?y where {
values ?x { 1 2 3 4 }
{
select ?y where {
values ?y { 5 6 7 8 }
}
limit 2
}
}
limit 5---------
| x | y |
=========
| 1 | 5 |
| 1 | 6 |
| 2 | 5 |
| 2 | 6 |
| 3 | 5 |
---------正如您所看到的,您从子查询中获得了两个值(5和6),并且这些值与外部查询中的绑定组合在一起,从外部查询中我们总共获得了5行(由于限制)。
子查询最先在最里面求值
但是,请记住,子查询是从最里面的第一个查询到最外面的查询。这意味着在您的查询中,
select ?person_id ?place_of_birth ?other_person_id
where {
?person_id fb:type.object.type fb:people.person .
?person_id fb:people.person.place_of_birth ?place_of_birth_id .
?place_of_birth_id fb:type.object.name ?place_of_birth .
{
select ?other_person_id
where {
?place_of_birth_id fb:location.location.people_born_here ?other_person_id .
}
LIMIT 1
}
FILTER (langMatches(lang(?place_of_birth),"en"))
}
LIMIT 3您正在查找一个匹配项
?place_of_birth_id fb:location.location.people_born_here ?other_person_id .并将?other_person_id绑定传递到外部查询中。但是,外部查询的其余部分不使用?other_person_id,,因此它实际上对结果没有任何影响。
该怎么做?
如果你只需要另外一个人
应用程序需要一个查询来返回每个人的出生地以及在同一地点出生的其他人的出生地。
从概念上讲,你可以把这看作是挑选一个人,找到他们的出生地,然后从出生在那个地方的人中再抽取一个人。实际上,您也可以像这样编写查询:
select ?person_id ?place_of_birth (sample(?other_person_idx) as ?other_person_id)
where {
?person_id fb:type.object.type fb:people.person .
?person_id fb:people.person.place_of_birth ?place_of_birth_id .
?place_of_birth_id fb:type.object.name ?place_of_birth .
FILTER (langMatches(lang(?place_of_birth),"en"))
?place_of_birth_id fb:location.location.people_born_here ?other_person_idx .
filter ( ?other_person_idx != ?person_id )
}
group by ?person_id ?place_of_birth如果您需要多个
如果每个结果都需要一个以上的“其他结果”,这就是一个棘手得多的问题。这就是Nested queries in sparql with limits的问题所在。在How to limit SPARQL solution group size?中有一种方法可以用于此目的。
https://stackoverflow.com/questions/27061096
复制相似问题