On 21/11/14 11:51, Paton, Diego wrote:
Hi,
I am developing a java application that uses ARQ to execute SPARQL
queries using a Fuseki endpoint over TDB.
The application needs a query that returns the place of birth of each
person and other person that was born in the same place.
To start, I wrote this SPARQL query that returns person_ids and the
place of birth of each person.
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 |
----------------------------------
After that, I added a subquery
(https://jena.apache.org/documentation/query/sub-select.html) adding
other person who was born there, but I get more than one person related
and I only need one.
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* |
---------------------------------------------------
I have tried to add a LIMIT 1 subquery but it seems that does not work (
the query is executed but never ends )
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
Therefore, is there a way to return only 1 result in the subquery ? or
can't do that using SPARQL.
Firstly, I hope you are using a reasonably recent version because there
was a fix in this area -- JENA-711
Second,
select ?other_person_id
so the inner select is returning a table of one column and that column
is not connected to anything in the outer query; ?other_person_id is
only mentioned in the outer select.
?place_of_birth_id in the inner query is not related to the
?place_of_birth_id in the outer pattern. They are in different scopes.
Putting it in the projection may help:
select ?other_person_id ?place_of_birth_id
although I'm not completely clear about the structure of your data
I completely different approach is to use GROUP BY / LIMIT but from your
description, the subquery, limit-by-resource pattern is what you want.
Andy
Thanks in advance,
Regards,
Diego.