transaction time out in sparql query - sparql

select distinct ?label ?resource count(distinct ?type) as ?score
where
{
values ?type
{ <http://dbpedia.org/class/yago/Abstraction100002137>
<http://dbpedia.org/class/yago/Company108058098>
<http://dbpedia.org/class/yago/ElectronicsCompany108003035>
<http://dbpedia.org/class/yago/Group100031264>
<http://dbpedia.org/class/yago/Institution108053576>
<http://dbpedia.org/class/yago/Organization108008335>
}
?resource rdfs:label ?label ;
foaf:name ?name ;
a ?type .
FILTER (lang(?label) = 'en').
}
ORDER BY DESC(?score)
limit 10
I am trying to run this query but it shows transaction timeout, I cut out many of these links from this query, but still it shows same issue

Related

Query in Sparql to get data of a person

I would like to have a list of all mountains names of the "dbo:firstAscentPerson" property of Lionel Terray (dbpedia: http://dbpedia.org/page/Lionel_Terray).
This below is what I have so far
select ?person ?firstAscentPerson where {
?person foaf:name "Lionel Terray"#en.
dbo:firstAscentPerson rdfs:label ?firstAscentPerson.
} LIMIT 100
The following query does what you need:
SELECT ?mountain ?mountainName ?person2 ?person2Name {
VALUES ?person1Name {"Lionel Terray"#en}
?mountain dbo:firstAscentPerson ?person1, ?person2 .
?mountain rdfs:label ?mountainName .
?person1 foaf:name ?person1Name .
FILTER (lang(?mountainName) = "en")
OPTIONAL {?person2 foaf:name ?person2Name . FILTER (lang(?person2Name) = 'en')}
}

Aggregate properties

I'm developing my own Fuseki endpoint from some DBpedia data.
I'm in doubt on how to aggregate properties related to a single resource.
SELECT ?name ?website ?abstract ?genre ?image
WHERE{
VALUES ?s {<http://dbpedia.org/resource/Attack_Attack!>}
?s foaf:name ?name ;
dbo:abstract ?abstract .
OPTIONAL { ?s dbo:genre ?genre } .
OPTIONAL { ?s dbp:website ?website } .
OPTIONAL { ?s dbo:image ?image } .
FILTER LANGMATCHES(LANG(?abstract ), "en")
}
SPARQL endpoint: http://dbpedia.org/sparql/
This query returns 2 matching results. They are different just for the dbo:genre value. There is a way I can query the knowledge base and retrieving a single result with a list of genres?
#chrisis's query works well on the DBpedia SPARQL Endpoint, which is based on Virtuoso.
However, if you are using Jena Fuseki, you should use more conformant syntax:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT
?name
(SAMPLE(?website) AS ?sample_website)
(SAMPLE(?abstract) AS ?sample_abstract)
(SAMPLE(?image) AS ?sample_image)
(GROUP_CONCAT(?genre; separator=', ') AS ?genres)
WHERE {
VALUES (?s) {(<http://dbpedia.org/resource/Attack_Attack!>)}
?s foaf:name ?name ;
dbo:abstract ?abstract .
OPTIONAL { ?s dbo:genre ?genre } .
OPTIONAL { ?s dbp:website ?website } .
OPTIONAL { ?s dbo:image ?image} .
FILTER LANGMATCHES(LANG(?abstract ), "en")
} GROUP BY ?name
The differences from the #chrisis's query are:
Since GROUP_CONCAT is an aggregation function, it might be used with GROUP BY only;
Since GROUP BY is used, all non-grouping variables should be aggregated (e.g. via SAMPLE);
GROUP_CONCAT syntax is slightly different.
In Fuseki, these AS in the projection are in fact superfluous: see this question and comments.
Yes, the GROUP_CONCAT() function is what you want.
SELECT ?name ?website ?abstract (GROUP_CONCAT(?genre,',') AS ?genres) ?image
WHERE{
<http://dbpedia.org/resource/Attack_Attack!> a dbo:Band ;
foaf:name ?name;
dbo:abstract ?abstract .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbo:genre ?genre } .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbp:website ?website} .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbo:image ?image} .
FILTER LANGMATCHES(LANG(?abstract ), "en")
}

Query for banks in DBpedia

Using http://dbpedia.org/sparql, I want to receive the geographic coordinates of all bank buildings. The list of classes tells me that I should query for Bank.
Yet, the following code yields nothing:
SELECT DISTINCT ?label ?lat ?long
WHERE {
[]
rdf:type dbpedia-owl:Bank ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label.
FILTER (LANGMATCHES(LANG(?label), 'en'))
}
If instead I query for any sibling to Bank, (e.g. Brewery or LawFirm), I see at least some results. What's wrong with above code?
If you look into a dbpedia page for a bank, you can see that instead on rdf:type, banks have a property dbpedia-owl:industry that has dbpedia:Bank (Refah bank) or dbpedia:Financial_services (Cyprus bank) as a value. So if you rewrite your query as the following, you will get some results:
SELECT DISTINCT ?label ?lat ?long
WHERE {
[] dbpedia-owl:industry dbpedia:Bank ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label.
FILTER (LANGMATCHES(LANG(?label), 'en'))
}
If you add dbpedia:Financial_services, other organisations such as London stock exchange will also appear:
SELECT DISTINCT ?bank
WHERE {
?bank dbpedia-owl:industry ?place ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label.
FILTER (?place in (dbpedia:Financial_services, dbpedia:Bank) &&
LANGMATCHES(LANG(?label), 'en'))
}
Again, by examining the London stock exchange, you can see that there is a product property that separates these financial institutions. So this will give you banks, but it might not cover all the banks available:
SELECT DISTINCT ?label ?lat ?long
WHERE {
[] dbpedia-owl:industry ?place ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label;
dbpedia-owl:product ?product.
FILTER ( ?place in (dbpedia:Financial_services, dbpedia:Bank)
&& ?product in (dbpedia:Bank, dbpedia:Private_banking, dbpedia:Professional_Banking, dbpedia:Retail_banking, dbpedia:Investment_banking, dbpedia:Commercial_bank)
&& LANGMATCHES(LANG(?label), "en"))
} ORDER BY DESC(COUNT(DISTINCT ?product))

How the pass the output of one sparql query as a input to another sparql query

I am trying get the dbpedia movie link using the movie name in the first query and pass that link in the second query to get the movies similar to this movie.For e.g Lagaan.Now instead of passing the link manually in the second query is there a way to combine the two queries and pass the output of first query as an input to the second query.i.e:the link of the movie lagaan.Also,if the first query gives multiple links eg:if i am searching for Harry potter it will return multiple harry potter series links so,it should handle that case as well.
Query1
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix dbpedia-owl: <http://dbpedia.org/ontology/>
select distinct ?film where {
?film a dbpedia-owl:Film .
?film rdfs:label ?label .
filter regex( str(?label), "Lagaan", "i")
}
limit 10
Query 2
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
select ?similar (count(?p) as ?similarity) where {
values ?movie { <http://dbpedia.org/resource/Lagaan> }
?similar ?p ?o ; a dbpedia-owl:Film .
?movie ?p ?o .
}
group by ?similar ?movie
having count(?p) > 35
order by desc(?similarity)
Edited query:
select ?film ?similar (count(?p) as ?similarity) where {
{
select distinct ?film where {
?film a dbpedia-owl:Film .
?film rdfs:label ?label .
filter regex( str(?label), "Lagaan", "i")
}
}
?similar ?p ?o ; a dbpedia-owl:Film .
?film ?p ?o .
}
group by ?similar ?film
having count(?p) > 35
order by desc(?similarity)
corrected query as told by Joshua Taylor
select ?film ?other (count(*) as ?similarity) {
{
select ?film where {
?film a dbpedia-owl:Film ; rdfs:label ?label .
filter contains(lcase(?label),"lagaan")
}
limit 1
}
?film ?p ?o .
?other a dbpedia-owl:Film ; ?p ?o .
}
group by ?film ?other
having count(?p) > 25
order by desc(?similarity)
is there a way to combine the two queries and pass the output of first
query as an input to the second query.
SPARQL 1.1 defines subqueries. The results of inner queries are available to outer queries, so they are "passed" to them. In your case, you would have something along the lines of:
select ?similarMovie (... as ?similarity) where {
{ #-- QUERY 1, find one or more films
select distinct ?film where {
#-- ...
}
}
#-- QUERY 2, find films similar to ?film
#-- ...
}

Printing matching properties in DBpedia query

The following query will search for matching subjects based on a list of properties and within a given distance. They are ranked by the number of matching properties (?numProperties), which is output as a number. How can I also print each of the properties that are matches?
Run query
select ?subject (count ( distinct ?property) as ?numProperties) ?label ?lat ?long where {
values ?property { dbpedia-owl:crosses dbpedia-owl:vehicle dbpedia-owl:reopened dbpedia-owl:years dbpedia-owl:access dbpedia-owl:third dbpedia-owl:time dbpedia-owl:construction dbpedia-owl:anniversary dbpedia-owl:series dbpedia-owl:length dbpprop:suspension dbpprop:bridge dbpprop:crosses dbpprop:City dbpprop:connecting dbpprop:last dbpprop:three dbpprop:suspension dbpprop:bridges dbpprop:built }
?subject ?property ?object .
?subject rdfs:label ?label .
?subject geo:lat ?lat .
?subject geo:long ?long .
FILTER (?long > -74.490898 && ?long < -73.490898 && ?lat > 40.207222 && ?lat < 41.207222 ) .
FILTER(langMatches(lang(?label),"EN")) .
}
group by ?subject ?label ?lat ?long
order by desc(?numProperties)
limit 15
#-- I took the liberty of tidying up your query a bit.
#-- The key is to use the GROUP_CONCAT aggregate function.
select
?subject
(count(distinct ?property) as ?numProperties)
?label
?lat
?long
#-- concatenate distinct properties into a ', ' separated string
(group_concat(distinct ?property;separator=', ') as ?properties)
where {
values ?property {
dbpedia-owl:crosses dbpedia-owl:vehicle dbpedia-owl:reopened
dbpedia-owl:years dbpedia-owl:access dbpedia-owl:third
dbpedia-owl:time dbpedia-owl:construction dbpedia-owl:anniversary
dbpedia-owl:series dbpedia-owl:length dbpprop:suspension
dbpprop:bridge dbpprop:crosses dbpprop:City dbpprop:connecting
dbpprop:last dbpprop:three dbpprop:suspension
dbpprop:bridges dbpprop:built
}
?subject ?property ?object ;
rdfs:label ?label ;
geo:lat ?lat ;geo:long ?long .
FILTER ( -74.490898 < ?long && ?long < -73.490898 &&
40.207222 < ?lat && ?lat < 41.207222 )
FILTER(langMatches(lang(?label),"EN"))
}
group by ?subject ?label ?lat ?long
order by desc(?numProperties)
limit 15
SPARQL results